OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to detect edges in image using python

  • Thread starter Thread starter ladies
  • Start date Start date
L

ladies

Guest
I'm having problems trying to detect edges in images corresponding to holes in a glass sample. Images look like this:

images

Single sample:

enter image description here

Each image contains part of a hole that was cut into a glass sample. Inspecting the images with my eyes, I can clearly see two regions, the glass and the hole. Sadly, all methods of trying to properly detect the edge haven't led to good results. The main reason why my attempts have failed, is the insufficient contrast between the glass and the hole, I believe. The hole is not cut through the entire thickness of the glass, leaving a glass bottom in the hole scattering light back into the camera thus making the contrast worse for me.

Image processing things I've already tried:

  • blurring (gaussian, bilateral)
  • sharpening
  • adjusting contrast
  • mean shift filtering
  • adaptive thresholding
  • canny edge detection
  • sobel edge detection
  • watershed segmentation

When taking the images I'm using an industrial camera with a ring light made of LEDs. The ring light can only be turned on or off, I can't adjust directions of the light or brightness. Taking images with various exposure times and analogue gains hasn't yielded much, since the contrast would stay the same throughout the measurements.

Does anyone have an idea what steps I could take in order to properly detect the edges in the images? Be it image processing, programming or tips on how to take better pictures, any idea is appreciated!

Here's an excerpt of my script:

Code:
import cv2
image = cv2.imread(
    r'path/to/images')
blurred = cv2.GaussianBlur(image, (7, 7), 9)
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255,
                            cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 71, 5)
edges = cv2.Canny(thresh, 100, 200)

contours, _ = cv2.findContours(
    edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

output_image = image.copy()
for contour in contours:
    cv2.drawContours(output_image, [contour], -1, (0, 255, 0), 2)
cv2.imshow('Circle Edge', cv2.resize(output_image, (1000, 1000)))
cv2.waitKey(0)
cv2.destroyAllWindows()

with the detected edges painted in green on the original image (found in the images in this post)

Thanks in advance!

mentioned in the previous text already various image processing / manipulation steps taken
<p>I'm having problems trying to detect edges in images corresponding to holes in a glass sample.
Images look like this:</p>
<p><a href="https://www.mediafire.com/folder/ym4579928popt/images" rel="nofollow noreferrer">images</a></p>
<p>Single sample:</p>
<p><a href="https://i.sstatic.net/JpByHQK2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JpByHQK2.png" alt="enter image description here" /></a></p>
<p>Each image contains part of a hole that was cut into a glass sample.
Inspecting the images with my eyes, I can clearly see two regions, the glass and the hole. Sadly, all methods of trying to properly detect the edge haven't led to good results. The main reason why my attempts have failed, is the insufficient contrast between the glass and the hole, I believe. The hole is not cut through the entire thickness of the glass, leaving a glass bottom in the hole scattering light back into the camera thus making the contrast worse for me.</p>
<p>Image processing things I've already tried:</p>
<ul>
<li>blurring (gaussian, bilateral)</li>
<li>sharpening</li>
<li>adjusting contrast</li>
<li>mean shift filtering</li>
<li>adaptive thresholding</li>
<li>canny edge detection</li>
<li>sobel edge detection</li>
<li>watershed segmentation</li>
</ul>
<p>When taking the images I'm using an industrial camera with a ring light made of LEDs. The ring light can only be turned on or off, I can't adjust directions of the light or brightness.
Taking images with various exposure times and analogue gains hasn't yielded much, since the contrast would stay the same throughout the measurements.</p>
<p>Does anyone have an idea what steps I could take in order to properly detect the edges in the images?
Be it image processing, programming or tips on how to take better pictures, any idea is appreciated!</p>
<p>Here's an excerpt of my script:</p>
<pre><code>import cv2
image = cv2.imread(
r'path/to/images')
blurred = cv2.GaussianBlur(image, (7, 7), 9)
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 71, 5)
edges = cv2.Canny(thresh, 100, 200)

contours, _ = cv2.findContours(
edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

output_image = image.copy()
for contour in contours:
cv2.drawContours(output_image, [contour], -1, (0, 255, 0), 2)
cv2.imshow('Circle Edge', cv2.resize(output_image, (1000, 1000)))
cv2.waitKey(0)
cv2.destroyAllWindows()
</code></pre>
<p>with the detected edges painted in green on the original image (found in the images in this post)</p>
<p>Thanks in advance!</p>
<p>mentioned in the previous text already
various image processing / manipulation steps taken</p>
 

Latest posts

Top