Python+OpenCV感兴趣区域ROI提取

Python+OpenCV感兴趣区域ROI提取

方法一:使用轮廓

步骤1

"""src为原图"""
ROI = np.zeros(src.shape, np.uint8)      #感兴趣区域ROI
proimage = src.copy()         #复制原图
"""提取轮廓"""  
proimage=cv2.cvtColor(proimage,cv2.COLOR_BGR2GRAY)                   #转换成灰度图
proimage=cv2.adaptiveThreshold(proimage,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,7,7)                       
proimage,contours,hierarchy=cv2.findContours(proimage,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_NONE) #提取所有的轮廓       

步骤2

"""ROI提取"""
cv2.drawContours(ROI, contours, 1,(255,255,255),-1)       #ROI区域填充白色,轮廓ID1
ROI=cv2.cvtColor(ROI,cv2.COLOR_BGR2GRAY)                   #转换成灰度图
ROI=cv2.adaptiveThreshold(ROI,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY_INV,7,7)                                     #自适应阈值化
imgroi= cv2.bitwise_and(ROI,proimage)                        #图像交运算 ,获取的是原图处理——提取轮廓后的ROI
2.#imgroi = cv2.bitwise_and(src,src,mask=ROI) 
3.#imgroi = ROI & src 无需灰度+阈值,获取的是原图中的ROI

方法二

img1 = cv2.imread('roi.jpg')
roi = img1[0:rows, 0:cols ]

你可能感兴趣的:(OpenCV,Python)