栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

opencv+python 图片分割

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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("sbphWcImg7_" + 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)
'''


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号