How can I detect and crop the rectangular frame in the image?



Sure, if there is an object that has distinct hue or value (brightness). For hue probably wraparound / range thresholds would be needed, and maybe some other color space (rgb2lab?) would be better for that.

Possible sure, but probably typically less useful / more difficult to define useful criteria than in more intuitive / physical spaces like brightness.

These are distances in pixels between 1 and however large your image is. If you know how large the pixels are you could calculate the distance in pixels from a user specified distance in meters. Or use a percentage of image size. However the “1” is more to deal with pixel noise and not really related to the image size / resolution.

1 Like

Thanks a lot for the detailed answers.

Thank you. I tried to use this command in the code in different forms and lines; however, there was an error:

                       mask = skimage.morphology.isotropic_opening(filled, ent3w1.get())
                       mask_temp = mask
                       mask = skimage.segmentation.clear_border(mask)
                       if mask.count_nonzero() < mask_temp.count_nonzero():
                           mask = mask_temp
                       masked_result = image_rgb.copy()
                       masked_result[~mask, :] = 0

Exception in Tkinter callback
Traceback (most recent call last):
File “C:\Users.…\Python310\lib\”, line 1921, in call
return self.func(*args)
File “C:\Users.…\”, line 146, in entw1
if mask.count_nonzero() < mask_temp.count_nonzero():
AttributeError: ‘numpy.ndarray’ object has no attribute ‘count_nonzero’

Also, without using such approaches, the output of the images which have intersected the image border is empty (or a completely black image). For instance, please see the below image in which the white ribbon tied to the quadrat has reached the image edge:

My mistake; it should be numpy.count_nonzero(mask) instead of mask.count_nonzero() etc.

1 Like