2

I want to find dim edges using Python.

Input images (100 X 100) :

enter image description here

enter image description here

enter image description here

It consists of several horizontal boards: top, middle, bottom.

I want to find middle board bounding box like:

enter image description here

I used several edge detection methods (prewitt_x, sobel_x, cv2.findContours) but cannot detect well.

Because edge btw black region and board region is dim.

How can I find bounding box like red box?

Code below is example using prewitt_x and cv2.findContours:

import cv2
import numpy as np

img = cv2.imread('my_dir/my_img.bmp',0)

# prewitts_x
kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
img_prewittx = cv2.filter2D(img, -1, kernelx)
img_prewittx_gray = cv2.cvtColor(img_prewittx, cv2.COLOR_BGR2GRAY)
cv2.imwrite('my_outdir/my_outimg.bmp',img_prewittx)

# cv2.findContours
image, contours, hierarchy = cv2.findContours(img_prewittx_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(cnt) for cnt in contours]
print(rects)

In fact, I don't want to use slower one like Canny detector.

Help me :)

5
  • 1
    I am not sure if I understood your problem. What do you mean by several boards? What exactly do you want to find? Anyway, your image seems very noise. Can't you change colors or apply a noise reduction filter? Commented Jun 11, 2018 at 14:29
  • You could blur the image with a huge kernel and then apply a spatial closing operation. You will not get the smallest cut but at least you could get the different regions. When I understand the problem it is independent of the x coordinate. Now you can search for the longest white are in both of the identified sets and adjust the y value. Commented Jun 11, 2018 at 15:32
  • @fredguth Each image, they have total 5 part. From up to down : gray-black-gray-black-gray. The gray part is what I mean "borad". What you mean "change color"? It's like using HSV filter? Commented Jun 15, 2018 at 8:28
  • @MaxKrappmann Do you mean a huge kernel is like [[10,10,10],[0,0,0],[10,10,10]] rather than [[1,1,1],[0,0,0],[1,1,1]] ? Commented Jun 15, 2018 at 8:31
  • No you have to increase your size of the filter. docs.opencv.org/3.1.0/d4/d13/tutorial_py_filtering.html Commented Jun 15, 2018 at 8:42

1 Answer 1

1

My suggestion:

  • use a simple edge detection filter such as Prewitt

  • project horizontally (sum of the pixels in every row)

  • analyze the resulting profile to detect the regions of low/high activity and delimit the desired slabs.

enter image description here

You can also try the maximum along rows instead of the sum.

But don't expect miracles, this is a hard problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your comment :) I'll try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.