findcontours
import numpy as np
import cv2
def motion_blur(image, degree=12, angle=45):
image = np.array(image)
# 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
motion_blur_kernel = np.diag(np.ones(degree))
motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
motion_blur_kernel = motion_blur_kernel / degree
blurred = cv2.filter2D(image, -1, motion_blur_kernel)
# convert to uint8
cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
blurred = np.array(blurred, dtype=np.uint8)
return blurred
import cv2
def motion_blur(image, degree=12, angle=45):
image = np.array(image)
# 这里生成任意角度的运动模糊kernel的矩阵, degree越大,模糊程度越高
M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
motion_blur_kernel = np.diag(np.ones(degree))
motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
motion_blur_kernel = motion_blur_kernel / degree
blurred = cv2.filter2D(image, -1, motion_blur_kernel)
# convert to uint8
cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
blurred = np.array(blurred, dtype=np.uint8)
return blurred
orgimg=cv2.imread("img.png",0)
im = cv2.imread('label.png')
orgrgbimg=cv2.imread("img.png")
cv2.imwrite("target.jpg",orgrgbimg)
if(0):
holeimg=cv2.imread("1141_1_1_X_551_Y_1701_qly_100_pass_subtpl_25_update_1153677968_13d17h53m10s.bmp",0)
holeimg=cv2.imread("1110_3_1_X_537_Y_1700_qly_100_fail_13d17h46m24s.bmp",0)
holegray=cv2.resize(holeimg,(holeimg.shape[1]*2,holeimg.shape[0]*2))
circles = cv2.HoughCircles(holeimg, cv2.HOUGH_GRADIENT, 10, 120, param1=50, param2=30, minRadius=0, maxRadius=10)
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(holegray, (i[0], i[1]), i[2], (0, 0, 255), -1)
#cv2.circle(holegray, (i[0], i[1]), 2, (255, 0, 255), -1)
cv2.imshow("circle", holegray)
cv2.waitKey(0)
#holeimg=cv2.imread("1110_3_1_X_537_Y_1700_qly_100_fail_13d17h46m24s.bmp",0)
holeimg = cv2.cvtColor(holeimg, cv2.COLOR_GRAY2BGR)
holeimg[holeimg<50]=0
cv2.imshow("holeimg", cv2.resize(holeimg,(holeimg.shape[1]*2,holeimg.shape[0]*2)))
cv2.waitKey(0)
immask=np.zeros_like(im)
orgrgbimg=cv2.imread("img.png")
cv2.imwrite("target.jpg",orgrgbimg)
if(0):
holeimg=cv2.imread("1141_1_1_X_551_Y_1701_qly_100_pass_subtpl_25_update_1153677968_13d17h53m10s.bmp",0)
holeimg=cv2.imread("1110_3_1_X_537_Y_1700_qly_100_fail_13d17h46m24s.bmp",0)
holegray=cv2.resize(holeimg,(holeimg.shape[1]*2,holeimg.shape[0]*2))
circles = cv2.HoughCircles(holeimg, cv2.HOUGH_GRADIENT, 10, 120, param1=50, param2=30, minRadius=0, maxRadius=10)
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv2.circle(holegray, (i[0], i[1]), i[2], (0, 0, 255), -1)
#cv2.circle(holegray, (i[0], i[1]), 2, (255, 0, 255), -1)
cv2.imshow("circle", holegray)
cv2.waitKey(0)
#holeimg=cv2.imread("1110_3_1_X_537_Y_1700_qly_100_fail_13d17h46m24s.bmp",0)
holeimg = cv2.cvtColor(holeimg, cv2.COLOR_GRAY2BGR)
holeimg[holeimg<50]=0
cv2.imshow("holeimg", cv2.resize(holeimg,(holeimg.shape[1]*2,holeimg.shape[0]*2)))
cv2.waitKey(0)
immask=np.zeros_like(im)
imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
maskgray=np.zeros_like(imgray)
#ret, thresh = cv2.threshold(imgray, 20, 255, 0)
immask[imgray>=0]=255
imgray[imgray>0]=255
imgray[imgray==0]=0
thresh=imgray
cv2.imwrite("bmask.jpg",thresh)
cv2.imshow("thresh",thresh)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,255,255),3)
#for py in range(0,len(contours)):
maskgray=np.zeros_like(imgray)
#ret, thresh = cv2.threshold(imgray, 20, 255, 0)
immask[imgray>=0]=255
imgray[imgray>0]=255
imgray[imgray==0]=0
thresh=imgray
cv2.imwrite("bmask.jpg",thresh)
cv2.imshow("thresh",thresh)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im,contours,-1,(0,255,255),3)
#for py in range(0,len(contours)):
y=np.random.randint(0, 30)
x=np.random.randint(30, 90)
x=0
#for i,cnt in enumerate(contours):
maskgray=np.ones_like(maskgray)
maskgray=np.empty_like(maskgray)
blackmask=np.zeros_like(immask)
x=np.random.randint(30, 90)
x=0
#for i,cnt in enumerate(contours):
maskgray=np.ones_like(maskgray)
maskgray=np.empty_like(maskgray)
blackmask=np.zeros_like(immask)
maskgray.fill(255)
cv2.imshow("img7", maskgray)
#maskgray=cv2.GaussianBlur(maskgray,(43, 43), 30)
maskgray=cv2.GaussianBlur(maskgray,(5, 5), 30)
cv2.imshow("img7", maskgray)
#maskgray=cv2.GaussianBlur(maskgray,(43, 43), 30)
maskgray=cv2.GaussianBlur(maskgray,(5, 5), 30)
#mask_blurred_3chan = cv2.cvtColor(mask_blurred, cv2.COLOR_GRAY2BGR).astype('float') / 255.
for i in range(len(contours)):
#cv2.drawContours(immask,[contours[i]],-1,(x+np.random.randint(0, 30),x+np.random.randint(0, 30),x+np.random.randint(0, 30)),cv2.FILLED)
cv2.drawContours(immask,[contours[i]],-1,(x,x,x),cv2.FILLED)
cv2.drawContours(blackmask,[contours[-11]],-1,(255,255,255),cv2.FILLED)
for i in range(len(contours)):
#cv2.drawContours(immask,[contours[i]],-1,(x+np.random.randint(0, 30),x+np.random.randint(0, 30),x+np.random.randint(0, 30)),cv2.FILLED)
cv2.drawContours(immask,[contours[i]],-1,(x,x,x),cv2.FILLED)
cv2.drawContours(blackmask,[contours[-11]],-1,(255,255,255),cv2.FILLED)
imgraymask = cv2.cvtColor(immask, cv2.COLOR_BGR2GRAY)
copyorg=orgimg.copy()
copyorg2=orgimg.copy()
copyorg3=orgimg.copy()
orgimg[imgraymask!=255]=imgraymask[imgraymask!=255]
copyorg=orgimg.copy()
copyorg2=orgimg.copy()
copyorg3=orgimg.copy()
orgimg[imgraymask!=255]=imgraymask[imgraymask!=255]
threshblur=cv2.GaussianBlur(thresh,(99,99),0)
mask_blurred=threshblur
mask_blurred_3chan = cv2.cvtColor(mask_blurred, cv2.COLOR_GRAY2BGR).astype('float') / 255.
#img = immask.astype('float') / 255.
bg = immask.astype('float') / 255.
#out = bg * (1 - mask_blurred_3chan)
out = bg * ( mask_blurred_3chan)
#out = bg * (1 - mask_blurred_3chan) + img * mask_blurred_3chan
out = (out * 255).astype('uint8')
cv2.imshow("stackblur", out)
cv2.waitKey(0)
cv2.imshow("stackthred", threshblur)
cv2.waitKey(0)
import pdb
pdb.set_trace()
mask_blurred=threshblur
mask_blurred_3chan = cv2.cvtColor(mask_blurred, cv2.COLOR_GRAY2BGR).astype('float') / 255.
#img = immask.astype('float') / 255.
bg = immask.astype('float') / 255.
#out = bg * (1 - mask_blurred_3chan)
out = bg * ( mask_blurred_3chan)
#out = bg * (1 - mask_blurred_3chan) + img * mask_blurred_3chan
out = (out * 255).astype('uint8')
cv2.imshow("stackblur", out)
cv2.waitKey(0)
cv2.imshow("stackthred", threshblur)
cv2.waitKey(0)
import pdb
pdb.set_trace()
imgraymask = cv2.cvtColor(immask, cv2.COLOR_BGR2GRAY)
motiongray=motion_blur(orgimg,5,30)
# rect = cv2.minAreaRect(cnt) # 最小外接矩形
# box = np.int0(cv2.boxPoints(rect)) # 矩形的四个角点取整
# cv2.drawContours(img_color1, [box], 0, (255, 0, 0), 2)
#motiongray=motion_blur(motiongray)
# x, y, w, h = cv2.boundingRect(cnt) # 外接矩形
# cv2.rectangle(img_color1, (x, y), (x + w, y + h), (0, 255, 0), 2)
guassiangray=cv2.GaussianBlur(orgimg,(5, 5), 30)
guassiangray=cv2.GaussianBlur(guassiangray,(5, 5), 30)
guassiangray=cv2.GaussianBlur(guassiangray,(5, 5), 30)
guassiangray=cv2.GaussianBlur(guassiangray,(11, 11), 0)
guassiangray=cv2.GaussianBlur(guassiangray,(21, 21), 0)
cvblurgray = cv2.blur(orgimg, (3, 3))
cvblurgray=cv2.GaussianBlur(cvblurgray,(5, 5), 5)
cvblurgray = cv2.medianBlur(cvblurgray, 7)
#guassiangray=cv2.GaussianBlur(guassiangray,(10, 10), 0)
copyorg[imgraymask!=255]=guassiangray[imgraymask!=255]
copyorg2[imgraymask!=255]=motiongray[imgraymask!=255]
copyorg3[imgraymask!=255]=cvblurgray[imgraymask!=255]
cv2.imshow("img", im)
width, height, channels = im.shape
center = (int(height/2), int(width/2))
cv2.imshow("guassiangray", copyorg)
cv2.imshow("img6", orgimg)
cv2.imshow("motiongray", copyorg2)
cv2.imwrite("source.jpg",immask)
cv2.imshow("img6", orgimg)
cv2.imshow("motiongray", copyorg2)
cv2.imwrite("source.jpg",immask)
cv2.imshow("img9", np.vstack([im,immask,blackmask]))
cv2.imshow("img10", orgrgbimg)
cv2.imshow("cv2.blur", copyorg3)
cv2.waitKey(0)
# immask = cv2.resize(immask,center)
# blackmask=cv2.resize(blackmask,center)
import pdb
#pdb.set_trace()
src_mask = np.zeros(blackmask.shape, blackmask.dtype)
poly = np.array([ [4,80], [30,54], [151,63], [160,37], [168,90], [172,134], [43,122] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
blackmask = 255 * np.ones(blackmask.shape, blackmask.dtype)
#blackmask=src_mask
normal_clone = cv2.seamlessClone(immask,orgrgbimg , blackmask, center, cv2.MIXED_CLONE)
cv2.imshow("img8", normal_clone)
cv2.imshow("img11", src_mask)
cv2.imshow("img12", blackmask)
cv2.waitKey(0)
cv2.imshow("img10", orgrgbimg)
cv2.imshow("cv2.blur", copyorg3)
cv2.waitKey(0)
# immask = cv2.resize(immask,center)
# blackmask=cv2.resize(blackmask,center)
import pdb
#pdb.set_trace()
src_mask = np.zeros(blackmask.shape, blackmask.dtype)
poly = np.array([ [4,80], [30,54], [151,63], [160,37], [168,90], [172,134], [43,122] ], np.int32)
cv2.fillPoly(src_mask, [poly], (255, 255, 255))
blackmask = 255 * np.ones(blackmask.shape, blackmask.dtype)
#blackmask=src_mask
normal_clone = cv2.seamlessClone(immask,orgrgbimg , blackmask, center, cv2.MIXED_CLONE)
cv2.imshow("img8", normal_clone)
cv2.imshow("img11", src_mask)
cv2.imshow("img12", blackmask)
cv2.waitKey(0)