我们从Python开源项目中,提取了以下33个代码示例,用于说明如何使用cv2.bitwise_not()。
def img_fill(im_in, n): # n = binary image threshold th, im_th = cv2.threshold(im_in, n, 255, cv2.THRESH_BINARY); # Copy the thresholded image. im_floodfill = im_th.copy() # Mask used to flood filling. # Notice the size needs to be 2 pixels than the image. h, w = im_th.shape[:2] mask = np.zeros((h + 2, w + 2), np.uint8) # Floodfill from point (0, 0) cv2.floodFill(im_floodfill, mask, (0, 0), 255); # Invert floodfilled image im_floodfill_inv = cv2.bitwise_not(im_floodfill) # Combine the two images to get the foreground. fill_image = im_th | im_floodfill_inv return fill_image
def color_picker(rect): global img,img_gray2,hsv roi=img[rect[0][1]:rect[1][1],rect[0][0]:rect[1][0]] b,g,r,_=np.uint8(cv2.mean(roi)) color=cv2.cvtColor(np.uint8([[[b,g,r]]]),cv2.COLOR_BGR2HSV) h= color[0][0][0] # define range of blue color in HSV lower = np.array([h-10,50,50]) upper = np.array([h+10,255,255]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv, lower, upper) # Bitwise-AND mask and original image res = cv2.bitwise_and(img,img, mask= mask) res2=cv2.bitwise_and(img_gray2,img_gray2, mask= cv2.bitwise_not(mask)) return res+res2
def select_largest_obj(self, img_bin, lab_val=255, fill_holes=False, smooth_boundary=False, kernel_size=15): '''Select the largest object from a binary image and optionally fill holes inside it and smooth its boundary. Args: img_bin (2D array): 2D numpy array of binary image. lab_val ([int]): integer value used for the label of the largest object. Default is 255. fill_holes ([boolean]): whether fill the holes inside the largest object or not. Default is false. smooth_boundary ([boolean]): whether smooth the boundary of the largest object using morphological opening or not. Default is false. kernel_size ([int]): the size of the kernel used for morphological operation. Default is 15. Returns: a binary image as a mask for the largest object. ''' n_labels, img_labeled, lab_stats, _ = \ cv2.connectedComponentsWithStats(img_bin, connectivity=8, ltype=cv2.CV_32S) largest_obj_lab = np.argmax(lab_stats[1:, 4]) + 1 largest_mask = np.zeros(img_bin.shape, dtype=np.uint8) largest_mask[img_labeled == largest_obj_lab] = lab_val # import pdb; pdb.set_trace() if fill_holes: bkg_locs = np.where(img_labeled == 0) bkg_seed = (bkg_locs[0][0], bkg_locs[1][0]) img_floodfill = largest_mask.copy() h_, w_ = largest_mask.shape mask_ = np.zeros((h_ + 2, w_ + 2), dtype=np.uint8) cv2.floodFill(img_floodfill, mask_, seedPoint=bkg_seed, newVal=lab_val) holes_mask = cv2.bitwise_not(img_floodfill) # mask of the holes. largest_mask = largest_mask + holes_mask if smooth_boundary: kernel_ = np.ones((kernel_size, kernel_size), dtype=np.uint8) largest_mask = cv2.morphologyEx(largest_mask, cv2.MORPH_OPEN, kernel_) return largest_mask
def img_fill(im_in,n): # n = binary image threshold th, im_th = cv2.threshold(im_in, n, 255, cv2.THRESH_BINARY); # Copy the thresholded image. im_floodfill = im_th.copy() # Mask used to flood filling. # Notice the size needs to be 2 pixels than the image. h, w = im_th.shape[:2] mask = np.zeros((h+2, w+2), np.uint8) # Floodfill from point (0, 0) cv2.floodFill(im_floodfill, mask, (0,0), 255); # Invert floodfilled image im_floodfill_inv = cv2.bitwise_not(im_floodfill) # Combine the two images to get the foreground. fill_image = im_th | im_floodfill_inv return fill_image
def __init__(self, srcimg): super(DataArgumentation, self).__init__( ) self.src = cv2.bitwise_not(srcimg) # opencv mat 1ch ?????????? # ???? self.transition_levels = [0, 3, 5, 6, 8, 10, 12] # ??? self.rotation_theta = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1]
def foreground(self, image, smooth=False, grayscale=False): """ Extract foreground from background :param image: :param smooth: :param grayscale: :return: """ if smooth and grayscale: image = self.toGrayscale(image) image = self.smooth(image) elif smooth: image = self.smooth(image) elif grayscale: image = self.toGrayscale(image) fgmask = self.fgbg.apply(image) ret, mask = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY_INV) mask_inv = cv2.bitwise_not(mask) return mask_inv
def overlay_img(self): """Overlay the transparent, transformed image of the arc onto our CV image""" #overlay the arc on the image rows, cols, channels = self.transformed.shape roi = self.cv_image[0:rows, 0:cols] #change arc_image to grayscale arc2gray = cv2.cvtColor(self.transformed, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(arc2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) #black out area of arc in ROI img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) img2_fg = cv2.bitwise_and(self.transformed, self.transformed, mask=mask) #put arc on ROI and modify the main image dst = cv2.add(img1_bg, img2_fg) self.cv_image[0:rows, 0:cols] = dst
def composite(img1, img2, mask0): if mask0.shape[2] == 3: mask2 = cv2.cvtColor(mask0, cv2.COLOR_BGR2GRAY) else: mask2 = mask0[:] mask1 = np.ones((img1.shape[0], img1.shape[1], 3), np.uint8) mask1[..., 0] = mask2 mask1[..., 1] = mask2 mask1[..., 2] = mask2 white = np.ones((img1.shape[0], img1.shape[1], 3), np.uint8) white[:] = (0, 0, 0) invmask = np.zeros((img1.shape[0], img1.shape[1], 3), np.uint8) invmask = cv2.absdiff(white, mask1) invmask = cv2.bitwise_not(invmask) output = np.zeros((img1.shape[0], img1.shape[1], 3), np.uint8) cv2.subtract(img2, invmask, dst=output) return output
def main(): # parse command line options if len(sys.argv) != 2: print 'Usage: python input_name output_name' exit(1) filePath = sys.argv[1] print "<----- processing %s ----->" % filePath #??????????????????????????????? img = cv2.imread(filePath, 0) img = cv2.resize(img, (1200, 900)) # ?????? # imgArr = np.array(img) # imgMean = np.mean(img) # imgcopy = imgArr - imgMean # imgcopy = imgcopy * 2 + imgMean * 3 # imgcopy = imgcopy / 255 canny = cv2.Canny(img, 60, 300) inverted = cv2.bitwise_not(canny) cv2.imshow('Canny', inverted) test1 = Image.fromarray(canny) test2 = Image.fromarray(inverted) result = pytesseract.image_to_string(test1, lang="eng", config="-c tessedit_char_whitelist=0123456789X") print result print "-------" result = pytesseract.image_to_string(test2, lang="eng") print result k = cv2.waitKey(0)
def fill(th, im_th): # Copy the thresholded image. im_floodfill = im_th.copy() # Mask used to flood filling. # Notice the size needs to be 2 pixels than the image. h, w = im_th.shape[:2] mask = np.zeros((h+2, w+2), np.uint8) # Floodfill from point (0, 0) cv2.floodFill(im_floodfill, mask, (0,0), 255); # Invert floodfilled image im_floodfill_inv = cv2.bitwise_not(im_floodfill); # Combine the two images to get the foreground. im_out = im_th | im_floodfill_inv return im_out;
def find_lines(img, acc_threshold=0.25, should_erode=True): if len(img.shape) == 3 and img.shape[2] == 3: # if it's color img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) img = cv2.GaussianBlur(img, (11, 11), 0) img = cv2.adaptiveThreshold( img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 2) img = cv2.bitwise_not(img) # thresh = 127 # edges = cv2.threshold(img, thresh, 255, cv2.THRESH_BINARY)[1] # edges = cv2.Canny(blur, 500, 500, apertureSize=3) if should_erode: element = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4)) img = cv2.erode(img, element) theta = np.pi/2000 angle_threshold = 2 horizontal = cv2.HoughLines( img, 1, theta, int(acc_threshold * img.shape[1]), min_theta=np.radians(90 - angle_threshold), max_theta=np.radians(90 + angle_threshold)) vertical = cv2.HoughLines( img, 1, theta, int(acc_threshold * img.shape[0]), min_theta=np.radians(-angle_threshold), max_theta=np.radians(angle_threshold), ) horizontal = list(horizontal) if horizontal is not None else [] vertical = list(vertical) if vertical is not None else [] horizontal = [line[0] for line in horizontal] vertical = [line[0] for line in vertical] horizontal = np.asarray(horizontal) vertical = np.asarray(vertical) return horizontal, vertical
def repaint_skin(filename): import cv2 shutil.copy(filename, filename + '.bak') frame = cv2.imread(filename) HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) l = np.array([0, 50, 80], dtype = "uint8") u = np.array([23, 255, 255], dtype = "uint8") skin_area = cv2.inRange(HSV, l, u) not_skin_area = cv2.bitwise_not(frame, frame, mask = skin_area) cv2.imwrite(filename, not_skin_area)
def animpingpong(self): obj=self.Object res=None for t in obj.OutList: print t.Label img=t.ViewObject.Proxy.img.copy() if res==None: res=img.copy() else: #rr=cv2.subtract(res,img) #rr=cv2.add(res,img) aw=0.0+float(obj.aWeight)/100 bw=0.0+float(obj.bWeight)/100 print aw print bw if obj.aInverse: # b umsetzen ret, mask = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY) img=cv2.bitwise_not(mask) rr=cv2.addWeighted(res,aw,img,bw,0) res=rr #b,g,r = cv2.split(res) cv2.imshow(obj.Label,res) #cv2.imshow(obj.Label +" b",b) #cv2.imshow(obj.Label + " g",g) #cv2.imshow(obj.Label + " r",r) res=img if not obj.matplotlib: cv2.imshow(obj.Label,img) else: from matplotlib import pyplot as plt # plt.subplot(121), plt.imshow(img,cmap = 'gray') plt.title(obj.Label), plt.xticks([]), plt.yticks([]) plt.show() self.img=img
def maskLogoOverImage(self): # Load two images img1 = cv2.imread('messi5.jpg') img2 = cv2.imread('opencv_logo.png') # I want to put logo on top-left corner, So I create a ROI rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols ] # Now create a mask of logo and create its inverse mask also img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) # Now black-out the area of logo in ROI img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) # Take only region of logo from logo image. img2_fg = cv2.bitwise_and(img2,img2,mask = mask) # Put logo in ROI and modify the main image dst = cv2.add(img1_bg,img2_fg) img1[0:rows, 0:cols ] = dst cv2.imshow('res',img1) cv2.waitKey(0) cv2.destroyAllWindows() ##################################################################################################################### # Prototypes & Convenient CLI/GUI Dispatcher to rebuild mental picture of where we are/repeat on new platforms. #####################################################################################################################
def grab_cut_mask(img_col, mask, debug=False): assert isinstance(img_col, numpy.ndarray), 'image must be a numpy array' assert isinstance(mask, numpy.ndarray), 'mask must be a numpy array' assert img_col.ndim == 3, 'skin detection can only work on color images' assert mask.ndim == 2, 'mask must be 2D' kernel = numpy.ones((50, 50), numpy.float32) / (50 * 50) dst = cv2.filter2D(mask, -1, kernel) dst[dst != 0] = 255 free = numpy.array(cv2.bitwise_not(dst), dtype=numpy.uint8) if debug: scripts.display('not skin', free) scripts.display('grabcut input', mask) grab_mask = numpy.zeros(mask.shape, dtype=numpy.uint8) grab_mask[:, :] = 2 grab_mask[mask == 255] = 1 grab_mask[free == 255] = 0 if numpy.unique(grab_mask).tolist() == [0, 1]: logger.debug('conducting grabcut') bgdModel = numpy.zeros((1, 65), numpy.float64) fgdModel = numpy.zeros((1, 65), numpy.float64) if img_col.size != 0: mask, bgdModel, fgdModel = cv2.grabCut(img_col, grab_mask, None, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK) mask = numpy.where((mask == 2) | (mask == 0), 0, 1).astype(numpy.uint8) else: logger.warning('img_col is empty') return mask
def convert_to_linedrawing(self, luminous_image_data): kernel = numpy.ones((3, 3), numpy.uint8) linedrawing = cv2.Canny(luminous_image_data, 5, 125) linedrawing = cv2.bitwise_not(linedrawing) linedrawing = cv2.erode(linedrawing, kernel, iterations=1) linedrawing = cv2.dilate(linedrawing, kernel, iterations=1) return linedrawing
def convert_to_linedrawing(self, luminous_image_data): neiborhood24 = numpy.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]], numpy.uint8) dilated = cv2.dilate(luminous_image_data, neiborhood24, iterations=1) diff = cv2.absdiff(dilated, luminous_image_data) linedrawing = cv2.bitwise_not(diff) return linedrawing
def extract_bv(image): clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) contrast_enhanced_green_fundus = clahe.apply(image) # applying alternate sequential filtering (3 times closing opening) r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1) R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1) r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1) R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1) r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1) R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1) f4 = cv2.subtract(R3,contrast_enhanced_green_fundus) f5 = clahe.apply(f4) # removing very small contours through area parameter noise removal ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY) mask = np.ones(f5.shape[:2], dtype="uint8") * 255 im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in contours: if cv2.contourArea(cnt) <= 200: cv2.drawContours(mask, [cnt], -1, 0, -1) im = cv2.bitwise_and(f5, f5, mask=mask) ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV) newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1) # removing blobs of microaneurysm & unwanted bigger chunks taking in consideration they are not straight lines like blood # vessels and also in an interval of area fundus_eroded = cv2.bitwise_not(newfin) xmask = np.ones(image.shape[:2], dtype="uint8") * 255 x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) for cnt in xcontours: shape = "unidentified" peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.04 * peri, False) if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100: shape = "circle" else: shape = "veins" if(shape=="circle"): cv2.drawContours(xmask, [cnt], -1, 0, -1) finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask) blood_vessels = cv2.bitwise_not(finimage) dilated = cv2.erode(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)), iterations=1) #dilated1 = cv2.dilate(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1) blood_vessels_1 = cv2.bitwise_not(dilated) return blood_vessels_1
def _filter_image(self, image): _, thresh = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY) opened = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, (5, 5), iterations=3) return cv2.bitwise_not(opened)
def find_contours(img): img1 = cv2.bitwise_not(img) imgray = cv2.cvtColor(img1 ,cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(imgray,127,255,0) contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) img_cont = img1.copy() cv2.drawContours(img_cont, contours, -1, (255,255,255), -1) return img_cont
def __add_figure_to_frame(self, frame, figure_location): """This function is used to add a file from hard disk to the figure Algorithm source: http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html """ # Get size of frame height, width, channels = frame.shape # Only add icon when the frame is big enough if height >= 100 and width >= 100: # Load heart icon icon_heart = cv2.imread(figure_location) # Convert to RGB icon_heart = cv2.cvtColor(icon_heart, cv2.COLOR_BGR2RGB) # Create ROI rows, cols, channels = icon_heart.shape roi = frame[:rows, :cols, :] # Convert heart to greyscale icon_heart_gray = cv2.cvtColor(icon_heart, cv2.COLOR_RGB2GRAY) # Create mask and inverse mask with binary threshold ret, mask = cv2.threshold(icon_heart_gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) # Background: Original frame with inverse mask frame_bg = cv2.bitwise_and(roi, roi, mask=mask_inv) # Foreground: Heart with normal mask icon_heart_fg = cv2.bitwise_and(icon_heart, icon_heart, mask=mask) # Add heart icon to frame icon_heart_final = cv2.add(frame_bg, icon_heart_fg) frame[:rows, :cols, :] = icon_heart_final return frame # Setter and getter following
def outlining(img): #kernel size kernel_size=3 #------------------------------------------------- #bilateral filter, sharpen, thresh image biblur=cv2.bilateralFilter(img,20,175,175) sharp=cv2.addWeighted(img,1.55,biblur,-0.5,0) ret1,thresh1 = cv2.threshold(sharp,127,255,cv2.THRESH_OTSU) #negative and closed image inv=cv2.bitwise_not(thresh1) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size)) closed = cv2.morphologyEx(inv, cv2.MORPH_CLOSE, kernel) return closed
def gen_random_image(): img = np.zeros((IMAGE_H, IMAGE_W, INPUT_CHANNELS), dtype=np.uint8) mask = np.zeros((IMAGE_H, IMAGE_W, NUMBER_OF_CLASSES), dtype=np.uint8) mask_obj1 = np.zeros((IMAGE_H, IMAGE_W, 1), dtype=np.uint8) colors = np.random.permutation(256) # Background img[:, :, 0] = colors[0] img[:, :, 1] = colors[1] img[:, :, 2] = colors[2] # Object class 1 obj1_color0 = colors[3] obj1_color1 = colors[4] obj1_color2 = colors[5] while(True): center_x = rn.randint(0, IMAGE_W) center_y = rn.randint(0, IMAGE_H) r_x = rn.randint(10, 50) r_y = rn.randint(10, 50) if(center_x+r_x < IMAGE_W and center_x-r_x > 0 and center_y+r_y < IMAGE_H and center_y-r_y > 0): cv2.ellipse(img, (int(center_x), int(center_y)), (int(r_x), int(r_y)), int(0), int(0), int(360), (int(obj1_color0), int(obj1_color1), int(obj1_color2)), int(-1)) cv2.ellipse(mask_obj1, (int(center_x), int(center_y)), (int(r_x), int(r_y)), int(0), int(0), int(360), int(255), int(-1)) break mask[:,:,0] = np.squeeze(mask_obj1) mask[:,:,1] = np.squeeze(cv2.bitwise_not(mask_obj1)) # White noise density = rn.uniform(0, 0.1) for i in range(IMAGE_H): for j in range(IMAGE_W): if rn.random() < density: img[i, j, 0] = rn.randint(0, 255) img[i, j, 1] = rn.randint(0, 255) img[i, j, 2] = rn.randint(0, 255) return img, mask
def find_samples_bounding_rect(path): min_w = 0 min_h = 0 print ('finding bounding box:') bar = progressbar.ProgressBar(maxval=num_classes*num_samples, widgets=[ ' [', progressbar.Timer(), '] ', progressbar.Bar(), ' (', progressbar.ETA(), ') ', ]) bar.start() counter = 0 for i in range(1, num_classes + 1): for j in range(1, num_samples + 1): filename = '{0}/Sample{1:03d}/img{1:03d}-{2:03d}.png'.format(path, i, j) # opencv read -> Gray Image -> Bounding Rect im = cv2.imread(filename) imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) imgray = cv2.bitwise_not(imgray) _, contours, _ = cv2.findContours(imgray, cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) _, _, w, h = cv2.boundingRect(contours[len(contours) - 1]) # find maximum resolution min_w = max(min_w, w) min_h = max(min_h, h) # update progress bar counter = counter + 1 bar.update(counter) bar.finish() return min_w, min_h
def find_digits(binary_img): inv = cv2.bitwise_not(binary_img) contours, hierarchy = cv2.findContours(inv, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) digits = [] for cnt in contours: area = cv2.contourArea(cnt) if area > 500: [x, y, w, h] = cv2.boundingRect(cnt) margin = 20 x -= margin y -= margin w += margin*2 h += margin*2 figure = binary_img[y: y + h, x: x + w] if figure.size > 0: digits.append({ 'image': figure, 'x': x, 'y': y, 'w': w, 'h': h, }) return digits
def resize_digits(digits): digits = map(itemgetter('image'), sorted(digits, key=itemgetter('x'))) blur_kernel = np.ones((4, 4), np.float32)/(4*4) erode_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) return [ cv2.resize( cv2.bitwise_not( cv2.filter2D( cv2.erode(digit, erode_kernel, iterations=1), -1, blur_kernel) ), (20, 20)) for digit in digits]
def overlayimg(back, fore, x, y, w, h): # Load two images img1 = np.array(back) img2 = np.array(fore) # create new dimensions r = float((h)) / img2.shape[1] dim = ((w), int(img2.shape[1] * r)) # Now create a mask of box and create its inverse mask also img2gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask) # resize box and masks resized_img2 = cv2.resize(img2, dim, interpolation=cv2.INTER_AREA) resized_mask = cv2.resize(mask, dim, interpolation=cv2.INTER_AREA) resized_mask_inv = cv2.resize(mask_inv, dim, interpolation=cv2.INTER_AREA) # I want to put box in co-ordinates, So I create a ROI rows, cols, channels = resized_img2.shape roi = img1[y:y+rows, x:x+cols] # Now black-out the area of box in ROI img1_bg = cv2.bitwise_and(roi, roi, mask=resized_mask_inv) # Take only region of box from box image. img2_fg = cv2.bitwise_and(resized_img2, resized_img2, mask=resized_mask) # Put box in ROI and modify the main image dst = cv2.add(img1_bg, img2_fg) img1[y:y+rows, x:x+cols] = dst return img1
def bit_not(self, frame): self._ndarray = cv2.bitwise_not(self.ndarray, frame.ndarray) self._contours = None # XOR this frame with another frame
def generate_bg(root_dir): cv2.namedWindow("frame") cv2.namedWindow("foregrond mask") cv2.namedWindow("groundtruth") cv2.namedWindow("background model") for __, dirnames_l0, __ in walklevel(root_dir, level = 0): for dirname_l0 in dirnames_l0: if not os.path.exists(root_dir + "/" + dirname_l0 + "/done") or os.path.isfile(root_dir + "/" + dirname_l0 + "/done"): print ("start dealing with " + dirname_l0) if not os.path.exists(root_dir + "/" + dirname_l0 + "/bg") or os.path.isfile(root_dir + "/" + dirname_l0 + "/bg"): os.makedirs(root_dir + "/" + dirname_l0 + "/bg") num = 1 bgs = libbgs.SuBSENSE() F = open(root_dir + "/" + dirname_l0 + "/temporalROI.txt", 'r') line = F.read().split(' ') begin = int(line[0]); end = int(line[1]) ROI_mask = cv2.imread(root_dir + "/" + dirname_l0 + "/ROI.bmp") while True: frame_filename = root_dir + "/" + dirname_l0 + "/input/" + num2filename(num, "in") + ".jpg" gt_filename = root_dir + "/" + dirname_l0 + "/groundtruth/" + num2filename(num, "gt") + ".png" if not os.path.isfile(frame_filename): num = num + 1 continue frame = cv2.imread(frame_filename) gt = cv2.imread(gt_filename) check = (frame[:,:,0] == frame[:,:,1]) if check.all(): frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) fg = cv2.bitwise_and(bgs.apply(frame), ROI_mask[:,:,0]) bg_model = bgs.getBackgroundModel() addition = 83*np.ones(fg.shape, dtype=np.uint8) fg_mask = cv2.add(fg, addition, mask=cv2.bitwise_not(ROI_mask[:,:,0])) fg_mask = cv2.add(fg_mask, fg) cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1) cv2.putText(frame, str(num), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0)) cv2.imshow("frame", frame) cv2.imshow("foregrond mask", fg_mask) cv2.imshow("groundtruth", gt) cv2.imshow("background model", bg_model) if (num >= begin) & (num <= end): bg_filename = root_dir + "/" + dirname_l0 + "/bg/" + num2filename(num, "bg") + ".jpg" cv2.imwrite(bg_filename, bg_model) num = num + 1 if num > end: print ("finish with " + dirname_l0) break cv2.waitKey(20) os.makedirs(root_dir + "/" + dirname_l0 + "/done")
def detection(self, hsv_image): """Check for detection in the image """ mask = cv2.inRange(hsv_image, self.color_low, self.color_high) if self.baseline_cnt > 0: nmask = cv2.bitwise_not(mask) if self.baseline is None: rospy.loginfo("getting baseline for {}".format(self.name)) self.baseline = nmask else: self.baseline = cv2.bitwise_or(nmask, self.baseline) mask = cv2.bitwise_and(mask, self.baseline) count = cv2.countNonZero(mask) + self.baseline_fuzzy self.low_count = max(self.low_count, count) self.high_count = self.low_count + self.baseline_fuzzy self.baseline_cnt -= 1 return elif self.baseline is not None: mask = cv2.bitwise_and(mask, self.baseline) count = cv2.countNonZero(mask) if count > self.low_count and self.active is None: self.active = rospy.get_rostime() rospy.loginfo("{} ACTIVE ({})".format(self.name, count)) self.cloud.reset() if self.callbacks[0] is not None: self.callbacks[0](self.name) elif self.active is not None and count < self.high_count: rospy.loginfo("{} GONE ({})".format(self.name, count)) self.cloud.reset() self.active = None if self.callbacks[2] is not None: self.published = False self.report_count = 0 if self.callbacks[1] is not None: self.callbacks[1](self.name) # DEBUGGING to see what the masked image for the request is if self.debug: cv2.namedWindow(self.name, cv2.WINDOW_NORMAL) if self.baseline is not None: cv2.namedWindow(self.name+'_baseline', cv2.WINDOW_NORMAL) cv2.imshow(self.name+'_baseline', self.baseline) cv2.imshow(self.name, mask) cv2.waitKey(1) # to to see if we notify the location callback if self.is_active() and self.report_count > self.min_reports: now = rospy.get_rostime() if (self.active + self.stablize_time) < now: self.published = True point = PointStamped() center = self.cloud.find_center() point.header.seq = 1 point.header.stamp = now point.header.frame_id = self.frame_id point.point.x = center[0] point.point.y = center[1] point.point.z = center[2] if self.callbacks[2] is not None: self.callbacks[2](self.name, point)
def gen_random_image(): img = np.zeros((IMAGE_H, IMAGE_W, INPUT_CHANNELS), dtype=np.uint8) mask = np.zeros((IMAGE_H, IMAGE_W, NUMBER_OF_CLASSES), dtype=np.uint8) mask_obj1 = np.zeros((IMAGE_H, IMAGE_W, 1), dtype=np.uint8) mask_obj2 = np.zeros((IMAGE_H, IMAGE_W, 1), dtype=np.uint8) colors = np.random.permutation(256) # Background img[:, :, 0] = colors[0] img[:, :, 1] = colors[1] img[:, :, 2] = colors[2] # Object class 1 obj1_color0 = colors[3] obj1_color1 = colors[4] obj1_color2 = colors[5] while(True): center_x = rn.randint(0, IMAGE_W) center_y = rn.randint(0, IMAGE_H) r_x = rn.randint(10, 50) r_y = rn.randint(10, 50) if(center_x+r_x < IMAGE_W and center_x-r_x > 0 and center_y+r_y < IMAGE_H and center_y-r_y > 0): cv2.ellipse(img, (int(center_x), int(center_y)), (int(r_x), int(r_y)), int(0), int(0), int(360), (int(obj1_color0), int(obj1_color1), int(obj1_color2)), int(-1)) cv2.ellipse(mask_obj1, (int(center_x), int(center_y)), (int(r_x), int(r_y)), int(0), int(0), int(360), int(255), int(-1)) break # Object class 2 obj2_color0 = colors[6] obj2_color1 = colors[7] obj2_color2 = colors[8] while(True): left = rn.randint(0, IMAGE_W) top = rn.randint(0, IMAGE_H) dw = rn.randint(int(10*math.pi), int(50*math.pi)) dh = rn.randint(int(10*math.pi), int(50*math.pi)) if(left+dw < IMAGE_W and top+dh < IMAGE_H): mask_obj2 = np.zeros((IMAGE_H, IMAGE_W, 1), dtype=np.uint8) cv2.rectangle(mask_obj2, (left, top), (left+dw, top+dh), 255, -1) if(np.sum(cv2.bitwise_and(mask_obj1,mask_obj2)) == 0): cv2.rectangle(img, (left, top), (left+dw, top+dh), (obj2_color0, obj2_color1, obj2_color2), -1) break mask[:,:,0] = np.squeeze(mask_obj1) mask[:,:,1] = np.squeeze(mask_obj2) mask[:,:,2] = cv2.bitwise_not(cv2.bitwise_or(mask_obj1,mask_obj2)) # White noise density = rn.uniform(0, 0.1) for i in range(IMAGE_H): for j in range(IMAGE_W): if rn.random() < density: img[i, j, 0] = rn.randint(0, 255) img[i, j, 1] = rn.randint(0, 255) img[i, j, 2] = rn.randint(0, 255) return img, mask
def image_postprocessing_depth(gray, depth, t_size_y, t_size_x, feedback, t): if feedback: cv2.imwrite('feedback/image_' + str(t) + '_gray_0_input.png', gray) cv2.imwrite('feedback/image_' + str(t) + '_depth_0_input.png', gray) # resize normal image gray = cv2.resize(gray, (t_size_y, t_size_x)) if feedback: cv2.imwrite('feedback/image_' + str(t) + '_gray_1_resize.png', gray) # resize depth image depth = cv2.resize(depth, (t_size_y, t_size_x)) if feedback: cv2.imwrite('feedback/image_' + str(t) + '_depth_1_resize.png', depth) # cut normal image gray = gray[t_size_y/2-1:-1,:] if feedback: cv2.imwrite('feedback/image_' + str(t) + '_gray_2_cut.png', gray) # cut depth image depth = depth[t_size_y/2-1:-1,:] if feedback: cv2.imwrite('feedback/image_' + str(t) + '_depth_2_cut.png', depth) # threshold filter for the grayscale image ret,gray = cv2.threshold(gray,160,255,cv2.THRESH_BINARY) if feedback: cv2.imwrite('feedback/image_' + str(t) + '_gray_3_flt.png', gray) # custom filter for the depth image depth = cv2.bitwise_not(depth) ret, depth = cv2.threshold(depth,165,255,cv2.THRESH_TOZERO) if feedback: cv2.imwrite('feedback/image_' + str(t) + '_depth_3_flt_inv.png', depth) height, width = depth.shape # subtract lowest gray-value minval = np.min(depth[np.nonzero(depth)]) depth[np.nonzero(depth)] -= minval if feedback: cv2.imwrite('feedback/image_' + str(t) + '_depth_4_off.png', depth) # return the added image result = cv2.add(gray,depth) if feedback: cv2.imwrite('feedback/image_' + str(t) + '_final.png', result) return result # calculates the gray-scale image from ViZDoom