python长方形图片_如何使用python在此图像中找到矩形形状?

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值