T
T
Timebird2017-11-09 21:51:05
Python
Timebird, 2017-11-09 21:51:05

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 # граница СВЕРХУ

But in this way I calculate the coordinates of the point where the frame ends only on the left-top. Everything would be fine, but even knowing these coordinates it does not reach - how to proceed further? The frame has different thicknesses from different sides.
Need to find the coordinates on the right-bottom? And how to make the cycle follow the image in this direction? Am I generally on the right track, or is the approach fundamentally wrong? Maybe there is a ready-made function for this?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
1
15432, 2017-11-09
@15432

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

D
Denis, 2017-11-10
@D3Nd3R

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;

D
Dimonchik, 2017-11-09
@dimonchik2013

cool guys use OpenCV
in my opinion, there are simpler examples, look for OpenCV + Frame / Border

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question