Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- img = cv2.imread('lena.jpg')
- mask = cv2.imread('mask.png',0)
- res = cv2.bitwise_and(img,img,mask = mask)
- # http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghcircles/py_houghcircles.html
- import cv2
- import numpy as np
- # load the image
- img = cv2.imread('E:\FOTOS\opencv\opencv_logo.png')
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
- # detect circles
- gray = cv2.medianBlur(cv2.cvtColor(img, cv2.COLOR_RGB2GRAY), 5)
- circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=50, minRadius=0, maxRadius=0)
- circles = np.uint16(np.around(circles))
- # draw mask
- mask = np.full((img.shape[0], img.shape[1]), 0, dtype=np.uint8) # mask is only
- for i in circles[0, :]:
- cv2.circle(mask, (i[0], i[1]), i[2], (255, 255, 255), -1)
- # get first masked value (foreground)
- fg = cv2.bitwise_or(img, img, mask=mask)
- # get second masked value (background) mask must be inverted
- mask = cv2.bitwise_not(mask)
- background = np.full(img.shape, 255, dtype=np.uint8)
- bk = cv2.bitwise_or(background, background, mask=mask)
- # combine foreground+background
- final = cv2.bitwise_or(fg, bk)
- assert len(mask.shape) == 2 and issubclass(mask.dtype.type, np.floating)
- assert len(foreground_rgb.shape) == 3
- assert len(background_rgb.shape) == 3
- alpha3 = np.stack([mask]*3, axis=2)
- blended = alpha3 * foreground_rgb + (1. - alpha3) * background_rgb
- alpha3 = np.squeeze(np.stack([np.atleast_3d(mask)]*3, axis=2))
- x, y = np.where(mask!=0)
- pts = zip(x, y)
- # Assuming dst and src are of same sizes
- for pt in pts:
- dst[pt] = src[pt]
- idx = (mask!=0)
- dst[idx] = src[idx]
- import cv2 as cv
- im_color = cv.imread("lena.png", cv.IMREAD_COLOR)
- im_gray = cv.cvtColor(im_color, cv.COLOR_BGR2GRAY)
- _, mask = cv.threshold(im_gray, thresh=180, maxval=255, type=cv.THRESH_BINARY)
- im_thresh_gray = cv.bitwise_and(im_gray, mask)
- mask3 = cv.cvtColor(mask, cv.COLOR_GRAY2BGR) # 3 channel mask
- im_thresh_color = cv.bitwise_and(im_color, mask3)
- cv.imshow("original image", im_color)
- cv.imshow("binary mask", mask)
- cv.imshow("3 channel mask", mask3)
- cv.imshow("im_thresh_gray", im_thresh_gray)
- cv.imshow("im_thresh_color", im_thresh_color)
- cv.waitKey(0)
Add Comment
Please, Sign In to add comment