用附近的颜色替换图像的痣

时间:2019-05-26 16:37:20

标签: python opencv image-processing cv2 skin

我有一个人像,想检测其中的痣,并用Opencv尝试使用的最接近肤色将它们隐藏起来。

image=cv2.imread("917716303.B07KMBYMFS.BACK.jpg")
%matplotlib inline
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
edged = cv2.Canny(gray, 50, 100)
cv2.imshow("Original", blur)

# find contours in the image and initialize the mask that will be
# used to remove the bad contours
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, 
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
mask = np.ones(blur.shape[:2], dtype="uint8") * 255

def is_contour_bad(c):
   value=(cv2.contourArea(c)>=0 and cv2.contourArea(c)<=200)
   return value

for c in cnts:
  # if the contour is bad, draw it on the mask
    if is_contour_bad(c):
        cv2.drawContours(mask, [c], -1, 0, -1)

image = cv2.bitwise_and(blur, blur, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("After", image)
plt.imsave("mask.jpg",cv2.cvtColor(mask.astype("uint8"), cv2.COLOR_BGR2RGB))

def neighbours(i,j,M,N,size=4):
    if size==4:
       if (i==0 and j==0):
        n=[(0,1), (1,0)]
    elif i==0 and j==N-1:
        n=[(0,N-2), (1,N-1)]
    elif i==M-1 and j==0:
        n=[(M-1,1), (M-2,0)]
    elif i==M-1 and j==N-1:
        n=[(M-1,N-2), (M-2,N-1)]
    elif i==0:
        n=[(0,j-1), (0,j+1), (1,j)]
    elif i==M-1:
        n=[(M-1,j-1), (M-1,j+1), (M-2,j)]
    elif j==0:
        n=[(i-1,0), (i+1,0), (i,1)]
    elif j==N-1:
        n=[(i-1,N-1), (i+1,N-1), (i,N-2)]
    else:
        n=[(i-1,j), (i+1,j), (i,j-1), (i,j+1)]
    return n

if size==8:
    if (i==0 and j==0):
        n=[(0,1), (1,0) , (1,1)]
    elif i==0 and j==N-1:
        n=[(0,N-2), (1,N-1),(1,N-2)]
    elif i==M-1 and j==0:
        n=[(M-1,1), (M-2,0),(M-2,1)]
    elif i==M-1 and j==N-1:
        n=[(M-1,N-2), (M-2,N-1),(M-2,N-2)]
    elif i==0:
        n=[(0,j-1), (0,j+1), (1,j),(1,j-1),(1,j+1)]
    elif i==M-1:
        n=[(M-1,j-1), (M-1,j+1), (M-2,j),(M-2,j-1),(M-2,j+1)]
    elif j==0:
        n=[(i-1,0), (i+1,0), (i,1),(i-1,1),(i+1,1)]
    elif j==N-1:
        n=[(i-1,N-1), (i+1,N-1), (i,N-2),(i-1,N-2),(i+1,N-2)]
    else:
        n=[(i-1,j), (i+1,j), (i,j-1), (i,j+1),(i-1,j-1),(i-1,j+1),(i+1,j-1),(i+1,j+1)]
    return n

0 个答案:

没有答案