Answer the question
In order to leave comments, you need to log in
How to find the border thickness in an image?
There is an image with a frame (the entire frame is the same color), the thickness of the frame at the top, bottom, left and right is different. You need to find the thickness on each side.
Please tell me the algorithm by which you can calculate the thickness of the frame if it does not have a fixed length.
I tried it myself:
for i in range(width): # Цикл идет СЛЕВА
for j in range(height): # Цикл идет СВЕРХУ
if not np.array_equal(img[i,j], border_color): # если вдруг пиксели стали не равны цвету рамки
border[0] = i # граница СЛЕВА
border[1] = j # граница СВЕРХУ
Answer the question
In order to leave comments, you need to log in
in my projects, I found each border separately. you also do not take into account the option that only one pixel in the line will be border_color, and you will consider this line as a frame. Need to check that all pixels in a particular row have a border_color
#find left border
left_border = width - 1
for i in range(0, width):
border_ended = False
for j in range(0, height): #we must ensure all pixels on this column have border_color
if not np.array_equal(img[i,j], border_color):
border_ended = True
left_border = i
break
if border_ended:
break
#find right border. same, but reversed, from right to left
right_border = 0
for i in reversed(range(0, width)):
border_ended = False
for j in range(0, height):
if not np.array_equal(img[i,j], border_color):
border_ended = True
right_border = i
break
if border_ended:
break
#top border
top_border = height-1
for j in range(0, height):
border_ended = False
for i in range(0, width):
if not np.array_equal(img[i,j], border_color):
border_ended = True
top_border = j
break
if border_ended:
break
#bottom border
bottom_border = height-1
for j in reversed(range(0, height)):
border_ended = False
for i in range(0, width):
if not np.array_equal(img[i,j], border_color):
border_ended = True
bottom_border = j
break
if border_ended:
break
We solve with openCV.
1. Thresholding (everything that != frame color -> 0, otherwise 1);
2. Find boundaries with canny;
3. Find the contours (findContours with RETR_EXTERNAL flag) find the longest one. This is supposed to be the inside of the frame.
4. Approximation of the contour (convexHull)
5. Knowing the upper left and lower right points, you can find the frame.
thickness left = Xmin;
top thickness = Ymin;
thickness right = w - Xmax;
bottom thickness = p - Ymax;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question