opencv+python 图片分割

import cv2
import numpy as np
import os

im = cv2.imread("lamp1.jpg")
sp = im.shape
sz1 = sp[0]#height(rows) of image
sz2 = sp[1]#width(colums) of image
sz3 = sp[2]#the pixels value is made up of three primary colors

图像大小

if(sz2>4000 and sz2<6000):  
     Wth = sz2/2
     Ht = sz1/2
elif(sz2>6000 and sz2<9000):
     Wth = sz2/3
     Ht = sz1/3
else:
     Wth = sz2
     Ht = sz1    

灰度

image = cv2.resize(im,(Wth,Ht),interpolation=cv2.INTER_CUBIC) #resize 1500,1000
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#cv2.imshow("Image", image)

Sobel算子

gradX = cv2.Sobel(gray, ddepth=cv2.cv.CV_32F, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(gray, ddepth=cv2.cv.CV_32F, dx=0, dy=1, ksize=-1)
# subtract the y-gradient from the x-gradient
gradient = cv2.subtract(gradX, gradY)
gradient = cv2.convertScaleAbs(gradient)

模糊和二值化

# blur and threshold the image
blurred = cv2.blur(gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 80, 255, cv2.THRESH_BINARY)  #ret 90

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# perform a series of erosions and dilations
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)

(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
'''
def Crop_Img(i):  
     c = sorted(cnts, key=cv2.contourArea, reverse=True)[i]
print c
'''
if (os.path.exists('sbphW')==False):
    os.mkdir('sbphW')

分割生成多边形

for n in range(0, len(cnts)):
     c = sorted(cnts, key=cv2.contourArea, reverse=True)[n]
# compute the rotated bounding box of the largest contour
     rect = cv2.minAreaRect(c)
     box = np.int0(cv2.cv.BoxPoints(rect))
# draw a bounding box arounded the detected barcode and display the image  
#     cv2.drawContours(image, [box], -1, (0, 255, 0), 3)
     Xs = [i[0] for i in box]
     Ys = [i[1] for i in box]
#     x1 = min(Xs)
#     x2 = max(Xs)
#     y1 = min(Ys)
#     y2 = max(Ys)
     x1 = min(Xs)*2
     x2 = max(Xs)*2
     y1 = min(Ys)*2
     y2 = max(Ys)*2
     hight = y2 - y1
     width = x2 - x1
#     cropImg = image[y1:y1+hight, x1:x1+width]
     cropImg = im[y1:y1+hight, x1:x1+width]  #origin image for cut

#     cv2.imshow("Image", image)
     if (hight > 30 and width > 30):
         cv2.imwrite("sbphW\cImg7_" + str(n)+ ".jpg", cropImg)
         cv2.waitKey(0)     
 


'''
# draw a bounding box arounded the detected barcode and display the image
cv2.drawContours(image, [box], -1, (0, 255, 0), 3)

Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
y1 = min(Ys)
y2 = max(Ys)
hight = y2 - y1
width = x2 - x1
cropImg = image[y1:y1+hight, x1:x1+width]

cv2.imshow("Image", image)
cv2.imwrite("cImg.jpg", cropImg)
cv2.waitKey(0)
'''


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值