OpenCV:删除所选区域的背景图像
OpenCV: Remove background image of the selected area
我想知道如何删除我已经生成的选定区域的背景图像。为了清楚地理解,我附上了我需要解决的问题的可视化。
黑色区域是选中区域,我要反转。所以选中的区域会变成彩色图片,背景会变成黑色。
这是我已经创建的简单代码:
import cv2
import numpy as np
pic= cv2.imread('dataset/set.jpeg')
pic = cv2.resize(pic, dsize=(500, 400), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),5)
_,thres = cv2.threshold(blur, 100,250, cv2.THRESH_TOZERO)
res = cv2.Canny(thres, 100, 200, L2gradient=True)
circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=15,minRadius=80,maxRadius=100)
for i in circles[0,:]:
# draw the outer circle
cv2.circle(pic,(int(i[0]),int(i[1])),int(i[2]),(255,255,255),2)
i = i.astype(int)
pic[i[1]-i[2]:i[1]+i[2], i[0]-i[2]:i[0]+i[2]] = 0
cv2.imshow('Hole',pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
问题出在这段代码中:
pic[i[1]-i[2]:i[1]+i[2], i[0]-i[2]:i[0]+i[2]] = 0
我正在将所选区域更改为黑色(零),但我不知道如何将其反转为唯一有颜色的区域,而让另一个区域成为黑色图像。请帮我解决这个问题,谢谢!!
问题已通过以下代码行创建蒙版并结合前景和背景解决:
import cv2
import numpy as np
pic= cv2.imread('Assets/set.jpeg')
pic = cv2.resize(pic, dsize=(500, 400), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),5)
_,thres = cv2.threshold(blur, 100,250, cv2.THRESH_TOZERO)
res = cv2.Canny(thres, 100, 250, L2gradient=True)
circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=15,minRadius=80,maxRadius=100)
circles = np.uint16(np.around(circles))
mask = np.full((res.shape[0], res.shape[1]), 1, dtype=np.uint8) # mask is only
clone = pic.copy()
for i in circles[0, :]:
cv2.circle(mask, (i[0], i[1]), i[2], (255, 255, 255), -1)
cv2.circle(clone, (i[0], i[1]), i[2], (255, 255, 255), 1)
# get first masked value (foreground)
fg = cv2.bitwise_or(res, res, mask=mask)
# get second masked value (background) mask must be inverted
mask = cv2.bitwise_not(mask)
background = np.full(res.shape, 255, dtype=np.uint8)
bk = cv2.bitwise_or(background, background, mask=mask)
# combine foreground+background
final = cv2.bitwise_or(fg, bk)
result = np.concatenate((res,final),axis=1)
cv2.imshow('Hole',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
没什么好问的,我会关闭这个问题。谢谢!!
我想知道如何删除我已经生成的选定区域的背景图像。为了清楚地理解,我附上了我需要解决的问题的可视化。
黑色区域是选中区域,我要反转。所以选中的区域会变成彩色图片,背景会变成黑色。
这是我已经创建的简单代码:
import cv2
import numpy as np
pic= cv2.imread('dataset/set.jpeg')
pic = cv2.resize(pic, dsize=(500, 400), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),5)
_,thres = cv2.threshold(blur, 100,250, cv2.THRESH_TOZERO)
res = cv2.Canny(thres, 100, 200, L2gradient=True)
circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=15,minRadius=80,maxRadius=100)
for i in circles[0,:]:
# draw the outer circle
cv2.circle(pic,(int(i[0]),int(i[1])),int(i[2]),(255,255,255),2)
i = i.astype(int)
pic[i[1]-i[2]:i[1]+i[2], i[0]-i[2]:i[0]+i[2]] = 0
cv2.imshow('Hole',pic)
cv2.waitKey(0)
cv2.destroyAllWindows()
问题出在这段代码中:
pic[i[1]-i[2]:i[1]+i[2], i[0]-i[2]:i[0]+i[2]] = 0
我正在将所选区域更改为黑色(零),但我不知道如何将其反转为唯一有颜色的区域,而让另一个区域成为黑色图像。请帮我解决这个问题,谢谢!!
问题已通过以下代码行创建蒙版并结合前景和背景解决:
import cv2
import numpy as np
pic= cv2.imread('Assets/set.jpeg')
pic = cv2.resize(pic, dsize=(500, 400), interpolation=cv2.INTER_CUBIC)
gray=cv2.cvtColor(pic,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),5)
_,thres = cv2.threshold(blur, 100,250, cv2.THRESH_TOZERO)
res = cv2.Canny(thres, 100, 250, L2gradient=True)
circles = cv2.HoughCircles(res,cv2.HOUGH_GRADIENT,1,20,param1=200,param2=15,minRadius=80,maxRadius=100)
circles = np.uint16(np.around(circles))
mask = np.full((res.shape[0], res.shape[1]), 1, dtype=np.uint8) # mask is only
clone = pic.copy()
for i in circles[0, :]:
cv2.circle(mask, (i[0], i[1]), i[2], (255, 255, 255), -1)
cv2.circle(clone, (i[0], i[1]), i[2], (255, 255, 255), 1)
# get first masked value (foreground)
fg = cv2.bitwise_or(res, res, mask=mask)
# get second masked value (background) mask must be inverted
mask = cv2.bitwise_not(mask)
background = np.full(res.shape, 255, dtype=np.uint8)
bk = cv2.bitwise_or(background, background, mask=mask)
# combine foreground+background
final = cv2.bitwise_or(fg, bk)
result = np.concatenate((res,final),axis=1)
cv2.imshow('Hole',result)
cv2.waitKey(0)
cv2.destroyAllWindows()
没什么好问的,我会关闭这个问题。谢谢!!