如何使用python在此图像中找到矩形形状? [英] How to find rectangle shape in this image using python?

查看:4746
本文介绍了如何使用python在此图像中找到矩形形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此输入图片说明,您可以帮我分割此图片中的矩形对象吗? ,尝试了otsu,但它不起作用,因为background和forground具有相同的值。

enter image description herecan you help me to segment rectangular objects in this image, tried otsu but it is not working because background and forground have same values.

还有其他方法可以做同样的事情。

is there any other method to do the same.

有人可以告诉我如何在这些图像中找到一个矩形对象吗?
图像是canny边缘检测的结果。其实我想在视频中跟踪这些矩形,如果你知道怎么做,请告诉我。
OR至少我想知道是否存在矩形。 在此输入图片说明

Can somebody please tell me how to find a rectangle object in these images? Images are results of canny edge detection. Actually I want to track these rectangles in a video, if you know how to do it please tell me. OR at least I want to find whether a rectangle is present or not. enter image description here

推荐答案

您可以查看像素的行和列。例如,矩形的顶部边框行包含的黑色像素比上面的行多得多。所以我建议你使用垂直(通过行)和水平(通过列)传递来查找边框。这是我的脚本:

You can look at rows and columns of pixels. For example, the top border row of your rectangle contains many more black pixels than the row above. So I would suggest you to use vertical (through rows) and horizontal (through columns) passes to find the borders. Here's my script to do it:

from PIL import Image

FACTOR = 1.5 # a threashold

img = Image.open("path/to/your/image")
pix = img.load()
size = img.size

# vertical pass
sum_color_arr = []
for row_num in xrange(size[1]):
    sum_color = 0 # calculating of brightness for each row separately
    for i in xrange(size[0]):
        sum_color += pix[i, row_num]
    sum_color_arr.append(sum_color)

for row_num in xrange(size[1] - 1):
    if sum_color_arr[row_num] > FACTOR * sum_color_arr[row_num + 1]:
        print "Top border: y =", (row_num + 1)
    if sum_color_arr[row_num + 1] > FACTOR * sum_color_arr[row_num]:
        print "Bottom border: y =", row_num

# horizontal pass
sum_color_arr = []
for col_num in xrange(size[0]):
    sum_color = 0 # calculating of brightness for each column separately
    for i in xrange(size[1]):
        sum_color += pix[col_num, i]
    sum_color_arr.append(sum_color)

for col_num in xrange(size[0] - 1):
    if sum_color_arr[col_num] > FACTOR * sum_color_arr[col_num + 1]:
        print "Left border: x =", (col_num + 1)
    if sum_color_arr[col_num + 1] > FACTOR * sum_color_arr[col_num]:
        print "Right border: x =", col_num

这篇关于如何使用python在此图像中找到矩形形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆