def show_HUD(image): # rectangle overlay = image.copy() opacity = 0.5 cv2.rectangle(overlay,(0,0),(viewport_w,240),(0,0,0),-1) # list setup col1,y,col2,y1 = 5,50,100,15 def write_Text(row,subject): cv2.putText(overlay, subject, (col1,row), font, 1.0, white) cv2.putText(overlay, log[subject], (col2, row), font, 1.0, white) # write text to overlay write_Text(y, 'pixels') write_Text(y + y1, 'threshold') write_Text(y + y1 * 2, 'ratio') write_Text(y + y1 * 3, 'last') write_Text(y + y1 * 4, 'now') write_Text(y + y1 * 5, 'model') write_Text(y + y1 * 6, 'layer') write_Text(y + y1 * 7, 'width') write_Text(y + y1 * 8, 'height') write_Text(y + y1 * 9, 'octave') write_Text(y + y1 * 10, 'iteration') cv2.putText(overlay, log['detect'], (5, 35), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0,255,0)) # add overlay back to source cv2.addWeighted(overlay, opacity, image, 1-opacity, 0, image)
def draw_all(imageForNet, heatmaps, currIndex, div=4., norm=False): netDecreaseFactor = float(imageForNet.shape[0]) / float(heatmaps.shape[2]) # 8 resized_heatmaps = np.zeros(shape=(heatmaps.shape[0], heatmaps.shape[1], imageForNet.shape[0], imageForNet.shape[1])) num_maps = heatmaps.shape[1] combined = None for i in range(0, num_maps): heatmap = heatmaps[0,i,:,:] resizedHeatmap = cv2.resize(heatmap, (0,0), fx=netDecreaseFactor, fy=netDecreaseFactor) minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(resizedHeatmap) if i==currIndex and currIndex >=0: resizedHeatmap = np.abs(resizedHeatmap) resizedHeatmap = (resizedHeatmap*255.).astype(dtype='uint8') im_color = cv2.applyColorMap(resizedHeatmap, cv2.COLORMAP_JET) resizedHeatmap = cv2.addWeighted(imageForNet, 1, im_color, 0.3, 0) cv2.circle(resizedHeatmap, (int(maxLoc[0]),int(maxLoc[1])), 5, (255,0,0), -1) return resizedHeatmap else: resizedHeatmap = np.abs(resizedHeatmap) if combined is None: combined = np.copy(resizedHeatmap); else: if i <= num_maps-2: combined += resizedHeatmap; if norm: combined = np.maximum(0, np.minimum(1, combined)); if currIndex < 0: combined /= div combined = (combined*255.).astype(dtype='uint8') im_color = cv2.applyColorMap(combined, cv2.COLORMAP_JET) combined = cv2.addWeighted(imageForNet, 0.5, im_color, 0.5, 0) cv2.circle(combined, (int(maxLoc[0]),int(maxLoc[1])), 5, (255,0,0), -1) return combined
def putTextAlpha(img, text, alpha, org, fontFace, fontScale, color, thickness): # , lineType=None ''' Extends cv2.putText with [alpha] argument ''' x, y = cv2.getTextSize(text, fontFace, fontScale, thickness)[0] ox, oy = org imgcut = img[oy - y - 3:oy, ox:ox + x] if img.ndim == 3: txtarr = np.zeros(shape=(y + 3, x, 3), dtype=np.uint8) else: txtarr = np.zeros(shape=(y + 3, x), dtype=np.uint8) cv2.putText(txtarr, text, (0, y), fontFace, fontScale, color, thickness=thickness #, lineType=lineType ) cv2.addWeighted(txtarr, alpha, imgcut, 1, 0, imgcut, -1) return img
def get_controller_image(movement_index, action_index): overlay = img.copy() output = img.copy() if action_index == 0: cv2.circle(overlay, B_center, button_radius, pressed_color, -1) elif action_index == 1: cv2.circle(overlay, A_center, button_radius, pressed_color, -1) elif action_index == 2: cv2.circle(overlay, Y_center, button_radius, pressed_color, -1) elif action_index == 3: cv2.circle(overlay, X_center, button_radius, pressed_color, -1) cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output) if movement_index == 0: cv2.line(output, up_horizontal_from, up_horizontal_to, pressed_color, 2) cv2.line(output, up_vertical_from, up_vertical_to, pressed_color, 2) elif movement_index == 1: cv2.line(output, down_horizontal_from, down_horizontal_to, pressed_color, 2) cv2.line(output, down_vertical_from, down_vertical_to, pressed_color, 2) elif movement_index == 2: cv2.line(output, left_horizontal_from, left_horizontal_to, pressed_color, 2) cv2.line(output, left_vertical_from, left_vertical_to, pressed_color, 2) elif movement_index == 3: cv2.line(output, right_horizontal_from, right_horizontal_to, pressed_color, 2) cv2.line(output, right_vertical_from, right_vertical_to, pressed_color, 2) return output # image_controller = get_controller_image(1, 1) # cv2.imshow('controller', image_controller) # cv2.waitKey(0)
def mask_helper(self, img, mask, opacity = 0.4): """ Returns image with mask highlighted in red at given opacity Parameters ---------- img : ndarray (h,w,3) Image to mask mask : ndarray (h,w) Mask of same shape as image containing either binary values or floats < 1 to mask img with opacity : float Opacity for mask overlay Returns ------- Image with mask overlayed according to pixel value in mask """ h,w,c = img.shape adj_mask = np.logical_not(mask).reshape(h,w,1) gap_image = img*adj_mask red_mask = ([255,0,0]*mask.reshape(h,w,1)).astype('uint8') masked_image = gap_image+red_mask output = img.copy() cv2.addWeighted(masked_image, opacity, output, 1.0-opacity, 0, output) return output
def visualise_measurements(self): if not self.current_data_visualised: self.current_data_visualised = True cnt = lambda x: np.rint(np.array(x)*self.fusion_model_params.cvscale).astype(int) img = np.zeros((self.particle_filter_model_param.world_dimensions[1]*self.fusion_model_params.cvscale, self.particle_filter_model_param.world_dimensions[0]*self.fusion_model_params.cvscale,3)).astype('uint8') img +=255 im_copies = [] #Draw Usable Area if self.particle_filter_model_param.world_usable_area != [[]]: cv2.polylines(img,[cnt(self.particle_filter_model_param.world_usable_area.exterior.coords[:])], isClosed=True, color=(0,0,0), thickness=2) #Add sensors for k, sensor_prop in self.sensor_properties.items(): img_copy = copy.deepcopy(img) cv2.fillPoly(img_copy, [cnt(self.sensor_properties[k].measurement_boundary_poly.exterior.coords[:])], (128,128,128)) im_copies.append(img_copy) max_w = np.min(np.max([p.w for p in self.pir_model.particles]),0) img_copy = copy.deepcopy(img) for particle in self.pir_model.particles: col = 255.*np.log((particle.w+1)/(max_w+1)) cv2.circle(img_copy, (int(particle.location.x*self.fusion_model_params.cvscale), int(particle.location.y*self.fusion_model_params.cvscale)),5,(0,col,0),-1) im_copies.append(img_copy) for i,overlay in enumerate(im_copies): opacity = 0.4 cv2.addWeighted(overlay, opacity, img, 1-opacity, 0, img) for sensor in self.sensor_properties.values(): cv2.circle(img,(int(sensor.location.x*self.fusion_model_params.cvscale), int(sensor.location.y*self.fusion_model_params.cvscale)),10,(0,0,255),-1) if(self.m_x != -1 and self.m_y != -1) and not (self.m_x is np.inf or self.m_y is np.inf): if self.good: cv2.circle(img,(int(self.m_x*self.fusion_model_params.cvscale), int(self.m_y*self.fusion_model_params.cvscale)), min(12,int(self.fusion_model_params.cvscale/8.)),(255,255,0),1) else: cv2.circle(img,(int(self.m_x*self.fusion_model_params.cvscale), int(self.m_y*self.fusion_model_params.cvscale)), min(12,int(self.fusion_model_params.cvscale/8.)),(64,64,0),1) cv2.imshow('test',cv2.flip(img,0)) cv2.waitKey(1) else: logging.debug('Data unchanged visualisation not updated.')
def callback(self,data): try: np_arr = np.fromstring(data.data,np.uint8) image_np = cv2.imdecode(np_arr,cv2.CV_LOAD_IMAGE_COLOR) new_im = image_np.astype('float32') rgb_out_32 = new_im except CvBridgeError as e: print(e) # (N/(N+1))*Old + (1/(N+1))*New = Average alpha = (self.num/(self.num + 1.)) beta = 1. - alpha if self.num == 0: self.average = new_im cv2.addWeighted(self.average,alpha,new_im,beta,0.0,rgb_out_32) self.num += 1 rgb_out = rgb_out_32.astype('uint8') self.average = rgb_out_32 msg = bridge.cv2_to_imgmsg(rgb_out,"bgr8") try: self.image_pub.publish(msg) except CvBridgeError as e: print(e)
def run( self ): time.sleep(60) while True: worldmap = self._worldmap.copy() worldmapCopy = self._worldmap.copy() with open('positionData.csv', 'r') as _file: entries = _file.readlines() for entry in entries: timeStamp, day, hour, minute, second, x, y = entry.strip().split(';') cv2.circle(worldmap, (int(x), int(y)), 10, (129, 255, 59), -1) cv2.circle(worldmap, (int(x), int(y)), 10, (0, 0, 0), 1) if not x: time.sleep(60) continue cv2.addWeighted(worldmap, 0.4, worldmapCopy, 1 - 0.4, 0, worldmapCopy) cropped = worldmapCopy[int(y)-250:int(y)+250, int(x)-500:int(x)+500] cv2.imwrite( 'mapcrop.png', cropped ) cv2.circle(worldmapCopy, (int(x), int(y)), 20, (0, 0, 255), -1) cv2.imwrite( 'maptest.png', worldmapCopy ) os.system('./pngcrush maptest.png mapcompressed.png') os.system('./pngcrush mapcrop.png mapcropcomp.png') os.system('cp mapcompressed.png www/map.png') os.system('cp mapcropcomp.png www/mapcrop.png') with open( 'www/conversionCenter', 'w' ) as _file: _file.write('{} {}'.format(x, y)) time.sleep(400)
def __get_local_avgimg(self, imgs, ratio=0): mix = imgs[0] mix = cv2.addWeighted(mix, ratio, mix, 0, 0) for img in imgs[1:]: mix = cv2.addWeighted(mix, 1, img, ratio, 0) diff = cv2.absdiff(imgs[-1], mix).sum() return diff
def _corner_highlight(frame,corners,opacity=0.3): """Highlight the detected corners of the QR code with different colors :frame: cv2 image :corners: list of QR corner contours :opacity: float :returns: cv2 image """ dupe=frame.copy() biggies=_big_corners(corners) quad=_outer_quad(biggies) #blue,green,red bgr=[(255,0,0),(0,255,0),(0,0,255)] for big,color in zip(biggies,bgr): cv2.drawContours(dupe,[big],-1,color,-1) #cv2.putText(frame,"X",tuple(temp[3]),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,0),thickness=2) cv2.addWeighted(dupe,opacity,frame,1-opacity,0,frame) cv2.drawContours(frame,_unrolled_corners(corners),-1,(0,0,0)) #print type(biggies) cv2.drawContours(frame,[quad],-1,(0,0,0)) return frame
def show_webcam(mirror=False): cam = cv2.VideoCapture(0) while True: ret_val, img_raw = cam.read() if mirror: img_raw = cv2.flip(img_raw, 1) img_overlay = np.zeros(img_raw.shape, np.uint8) cv2.circle(img_overlay, (img_raw.shape[1]/2, img_raw.shape[0]/2), CURSOR_RADIUS, OVERLAY_COLOR, thickness=-1) cv2.rectangle(img_overlay, (100, 100), (120, 110), OVERLAY_COLOR, thickness=-1) cv2.rectangle(img_overlay, (400, 100), (420, 110), OVERLAY_COLOR, thickness=-1) cv2.putText(img_overlay, 'Sample text', (200, 200), cv2.FONT_HERSHEY_PLAIN, 2, OVERLAY_COLOR, thickness=2) #img_final = 0.5 * img_overlay + img_raw img_final = np.zeros(img_raw.shape, np.uint8) cv2.addWeighted(img_raw, 1.0, img_overlay, 0.5, 0.0, img_final) # Display the image cv2.imshow('my webcam', img_final) if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def compute_score_cnt(img, cnt, img_ref, cnt_ref, anchor_im='centroid', anchor_ref='centroid', trfm=(0,0,0), show=False, showImg=False, outputD=False, title='compute_score_cnt', variant=0): """ Align the centroids of both images and apply transform around the centroid of the first image, then compute score. """ img, cnt = pad_image_cnt(img, cnt, anchor_im) img_ref, cnt_ref = pad_image_cnt(img_ref, cnt_ref, anchor_ref) img_warp, cnt_t = transform_cnt(img, cnt, trfm, 'center') if show: vis1 = draw_contours(cnt_t, (800, 800), show=False, color=(0,0,255)) vis2 = draw_contours(cnt_ref, (800, 800), show=False, color=(0,255,0)) vis = cv2.addWeighted(vis1, 0.5, vis2, 0.5, 0) if showImg: img_warp_color = cv2.cvtColor(img_warp, cv2.cv.CV_GRAY2RGB) img_ref_color = cv2.cvtColor(img_ref, cv2.cv.CV_GRAY2RGB) visImg = cv2.addWeighted(img_warp_color, 0.5, img_ref_color, 0.5, 0) vis = cv2.addWeighted(vis, 1, visImg, 1, 0) cv2.imshow(title, vis) # cv2.waitKey() if outputD: score, D = shape_score_cnt(cnt_t, cnt_ref, outputD, title,variant=variant) return score, D else: score = shape_score_cnt(cnt_t, cnt_ref, outputD, title,variant=variant) return score
def draw_histogram(self): canvas=np.zeros((300,256,3),'uint8') temp1=np.zeros((300,256,3),'uint8') temp2=np.zeros((300,256,3),'uint8') temp3=np.zeros((300,256,3),'uint8') x=0 for pt in self.hist[2]: cv2.line(temp1, (x,100), (x,100-pt), (255,0,0)) x+=1 x=0 for pt in self.hist[1]: cv2.line(temp2, (x,200), (x,200-pt), (0,255,0)) x+=1 x=0 for pt in self.hist[0]: cv2.line(temp3, (x,300), (x,300-pt), (0,0,255)) x+=1 canvas=cv2.addWeighted(temp1,1.0, temp2, 1.0,0) canvas=cv2.addWeighted(canvas,1.0,temp3,1.0,0) return canvas
def click_and_crop(event, x, y, flags, param): # grab references to the global variables global refPt, cropping,blackBox # if the left mouse button was clicked, record the starting # (x, y) coordinates and indicate that cropping is being # performed if event == cv2.EVENT_LBUTTONDOWN: refPt = [(x, y)] cropping = True elif event == cv2.EVENT_MOUSEMOVE and len(refPt) ==1: newxy=[(x,y)] cv2.rectangle(image, refPt[0], newxy[0], (0, 255, 0), -1) blackBox=[(refPt[0]),(newxy[0])] # apply the overlay overlay=clone.copy() cv2.addWeighted(overlay, 0.5, image, 0.5,0, image) ##cv2.imshow("image", image) # check to see if the left mouse button was released elif event == cv2.EVENT_LBUTTONUP: # record the ending (x, y) coordinates and indicate that # the cropping operation is finished refPt.append((x, y)) cropping = False
def test_func(src1, src2, alpha=1.0, **kwargs): beta = 1.0 - alpha src1 = vt.rectify_to_float01(src1) src2 = vt.rectify_to_float01(src2) dst = np.empty(src1.shape, dtype=src1.dtype) cv2.addWeighted(src1=src1, src2=src2, dst=dst, alpha=alpha, beta=beta, dtype=-1, **kwargs) return dst
def mergeImages(img1, img2, AlignMethod='', Jacobian='', **kwargs): (kp1Matches, kp2Matches) = Alignment2D.ExtractFeatures(img1, img2, **kwargs) Transform = Alignment2D.AlignImages(kp1Matches, kp2Matches, AlignMethod, Jacobian) #Overlay the two images, showing the detected feature. rows,cols,colours = img1.shape Canvas1 = np.zeros((rows * 2, cols * 2, colours) , img1.dtype) Canvas2 = np.copy(Canvas1) finalRows, finalCols, colours = Canvas1.shape tx = cols/2; # Translate to the center of the canvas ty = rows/2; M = np.float32([[1,0,tx],[0,1,ty],[0,0,1]]) img3 = cv2.drawKeypoints(img1, kp1Matches,color=(0,0,255)) cv2.warpPerspective(img3, M, (finalCols, finalRows), Canvas1) finalTransform = np.dot(M, Transform) ; # Translate to the center of the canvas img2 = cv2.drawKeypoints(img2, kp2Matches,color=(255,0,0)) cv2.warpPerspective(img2, finalTransform, (finalCols, finalRows), Canvas2, borderMode=cv2.BORDER_TRANSPARENT) alpha = 0.5 beta = (1.0 - alpha) cv2.addWeighted(Canvas1, alpha, Canvas2, beta, 0.0, Canvas1) return Canvas1
def transparent_circle(img, center, radius, color, thickness): center = tuple(map(int, center)) assert len(color) == 4 and all( type(c) == float and 0.0 <= c <= 1.0 for c in color) bgr = [255 * c for c in color[:3]] # convert to 0-255 scale for OpenCV alpha = color[-1] radius = int(radius) if thickness > 0: pad = radius + 2 + thickness else: pad = radius + 3 roi = ( slice(center[1] - pad, center[1] + pad), slice(center[0] - pad, center[0] + pad), ) try: overlay = img[roi].copy() cv2.circle(img, center, radius, bgr, thickness=thickness, lineType=cv2.LINE_AA) opacity = alpha cv2.addWeighted( src1=img[roi], alpha=opacity, src2=overlay, beta=1.0 - opacity, gamma=0, dst=img[roi], ) except: logger.debug( "transparent_circle would have been partially outside of img. Did not draw it." )
def sam_circle(win, x, y, w, h, sub_type): circle_col = (214,214,214) outercircle_col = (10,10,10) accentcircle_col = (200,200,200) triangle_colour = (000,000,255) if w > h: r = w else: r = h tran1 = win.copy() tran2 = win.copy() trant = win.copy() cv2.circle(tran1, (x+int(round(.5*w)), y+int(round(.5*h))), int(round(0.9*r)), circle_col, 40) cv2.circle(tran1, (x+int(round(.5*w)), y+int(round(.5*h))), int(round(1*r))+20, outercircle_col, 30) cv2.circle(tran2, (x+int(round(.5*w)), y+int(round(.5*h))), int(round(1*r))+20, accentcircle_col, 2) trant = cv2.addWeighted(tran1, .3, tran2, .7, 0, trant) win = cv2.addWeighted(trant, .7, win, .3, 0, win) # cv2.line(win, (x-int(round(.5*w)), y-int(round(.5*h))), (x+w+int(round(.5*w)), y-int(round(.5*h))), triangle_colour, 3) # cv2.line(win, (x-int(round(.5*w)), y-int(round(.5*h))), (x+int(round(.5*w)), y+h+int(round(.5*h))), triangle_colour, 3) # cv2.line(win, (x+w+int(round(.5*w)), y-int(round(.5*h))), (x+int(round(.5*w)), y+h+int(round(.5*h))), triangle_colour, 3) cv2.line(win, (x-int(round(.25*r)), y), (x+w+int(round(.25*r)), y), triangle_colour, 3) cv2.line(win, (x-int(round(.25*r)), y), (x+int(round(.5*r)), y+h+int(round(.4*r))), triangle_colour, 3) cv2.line(win, (x+w+int(round(.25*r)), y), (x+int(round(.5*r)), y+h+int(round(.4*r))), triangle_colour, 3)
def initCalScreen(self): if self.calibrateCount == 0: cv2.circle(self.oframe, (250,250), self.cursorRadius, (0, 0, 255), thickness=-1) self.centX, self.centY = 250, 250 elif self.calibrateCount == 1: cv2.circle(self.oframe, (100,100), self.cursorRadius, (0, 0, 255), thickness=-1) self.centX, self.centY = 100, 100 elif self.calibrateCount == 2: cv2.circle(self.oframe, (100,400), self.cursorRadius, (0, 0, 255), thickness=-1) self.centX, self.centY = 100, 400 elif self.calibrateCount == 3: cv2.circle(self.oframe, (400,100), self.cursorRadius, (0, 0, 255), thickness=-1) self.centX, self.centY = 400, 100 elif self.calibrateCount == 4: cv2.circle(self.oframe, (400,400),self.cursorRadius, (0, 0, 255), thickness=-1) self.centX, self.centY = 400, 400 cv2.addWeighted(self.oframe, self.opacity, self.frame, 1 - self.opacity, 0, self.frame) cv2.putText(self.frame,'Hello, Welcome to 2.5D Editor', (self.width/16, self.height/16), self.font, 2.25,(255,255,255)) cv2.putText(self.frame,"Use your webcam and line up an item to use as your cursor within the circle", (self.width/16, (self.height*14)/16), self.font, 0.9,(255,255,255)) cv2.putText(self.frame,"Press the spacebar to begin calibrating your cursor, and use 'w' and 's' to adjust the cursor size", (self.width/16, (self.height*15)/16), self.font, 0.7,(255,255,255))
def main(): ''' print("Python : %s " % sys.version) print("OpenCV : %s " % cv2.__version__) print("Numpy : %s " % numpy.__version__) print("Matplotlib: %s " % matplotlib.__version__) ''' #画像表示 img = cv2.imread("data/face.jpg") #cv2.imshow('res', img) #cv2.waitKey(0) #cv2.destroyAllWindows() #cv2.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) #show() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Turn off the axis sobeled_x = cv2.Sobel(gray, cv2.CV_32F, 1, 0) sobeled_y = cv2.Sobel(gray, cv2.CV_32F, 0, 1) cv2.imshow('gray_sobel_edge', sobeled_x) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imshow('gray_sobel_edge', sobeled_y) cv2.waitKey(0) cv2.destroyAllWindows() gray_abs_sobelx = cv2.convertScaleAbs(sobeled_x) gray_abs_sobely = cv2.convertScaleAbs(sobeled_y) gray_sobel_edge = cv2.addWeighted(gray_abs_sobelx,0.5,gray_abs_sobely,0.5,0) cv2.imshow('gray_sobel_edge',gray_sobel_edge) cv2.waitKey(0) cv2.destroyAllWindows() laplace = cv2.Laplacian(gray, cv2.CV_64F) img64 = numpy.double(img) abs(laplace) img64[:,:,2] += cv2.max(abs(laplace) - 200, 0) * 4 numpy.clip(img64, 0, 255, out = img64) img = img64.astype('uint8') gray_sobel_edge = cv2.cvtColor(gray_sobel_edge, cv2.COLOR_GRAY2BGR) img = cv2.addWeighted(img, 0.5,gray_sobel_edge,0.5,0) plt.axis('off') plt.title("Input Stream") plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) plt.show() '''
def cv_mean(image_iterator, n_images): """ Compute the element-wise mean over an image iterator containing n_images """ w = 1. / n_images start = cv2.addWeighted(image_iterator.next(), w, image_iterator.next(), w, 0) return reduce(lambda x, y: cv2.addWeighted(x, 1, y, w, 0), image_iterator, start)
def recolor_rc(src, dst): """Simulate conversion from BGR to RC (red, cyan). (b, g, r) -> (0.5b + 0.5g, 0.5b + 0.5g, r) """ b, g, r = cv2.split(src) cv2.addWeighted(b, 0.5, g, 0.5, b) cv2.merge((b, b, r), dst)
def averageImages(imgList): """ receive a list of opencv images(numpy arrays) of the same size and sum them all with equal weight equal to 1.0/len(imgList). The dtype is set to np.uint8. The list is assumed to have lenght greater or equal to than 2.""" alpha = 1.0/len(imgList) average = cv2.addWeighted(imgList[0], alpha, imgList[1], alpha, 0) for i in range(3, len(imgList)): average = cv2.addWeighted(average, 1.0, imgList[i], alpha, 0) return average
def toColoursRC(self): if not self._isColour: return None else: b, g, r = cv2.split(self._image) cv2.addWeighted(b, 0.5, g, 0.5, 0, b) return OpenCvImage(cv2.merge((b, b, r)))
def draw_bubble(self, img): overlay = img.copy() if self.living: cv2.circle(overlay, (int(self.x), int(self.y)), 50, (255,255,255), -1) else: cv2.circle(overlay, (int(self.x), int(self.y)), 50, (0, 0, 0), -1) opacity = 0.4 cv2.addWeighted(overlay, opacity, img, 1 - opacity, 0, img)
def processframe(frameNum,frame,cap,watchPoints): print "frame %d time %d " % (frameNum,cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC)) stageChange = 0 vid_width = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) vid_height = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) cv2.rectangle(frame,(0,vid_height-50),(vid_width,vid_height),(0,0,0),-1) cv2.putText(frame, "Time: %0.1f Frame: %d File: %s" % ((cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC)/1000),frameNum,file_tag), (15,vid_height-25), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255,255,255,255)) for i, w in enumerate(watchPoints): a = frame[w.y-5:w.y+5, w.x-5:w.x+5] offset = i * 10; #frame[offset:offset+50,0:50] = a if True: redlow = (0,0,220) redhigh = (128,128,255) mask = cv2.inRange(a,redlow,redhigh) prevRed = w.isRed if np.count_nonzero(mask) > 0: w.isRed = 1 else: w.isRed = 0 same = (w.isRed == prevRed) if same == 0: if w.isRed: w.redStart = cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC) else: wasRedFor = cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC) - w.redStart print "RED TO GREEN detected; wasRedFor: %d" % wasRedFor print "offset:%d prevRed:%d nowRed:%d same:%d" % (i,prevRed,w.isRed,same) stageChange = 1 output = cv2.bitwise_and(a, a, mask = mask) frame[offset:offset+10,0:10] = output frame[offset:offset+10,10:20] = a col = (0,0,255) if w.isRed: col = (0,0,255) else: col = (255,0,0) cv2.rectangle(frame,(w.x-10,0),(w.x+10,w.y-10),col,-1) if w.isRed: redFor = cap.get(cv2.cv.CV_CAP_PROP_POS_MSEC) - w.redStart cv2.rectangle(frame,(w.x-10,w.y-40),(w.x+80,w.y-10),(0,0,0),-1) cv2.putText(frame, "%0.1f" % (redFor/1000), (w.x+15,w.y-15), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,255,255)) if False: opacity = 0.5 overlay = frame.copy() cv2.circle(overlay,(w.x,w.y),watchRadius,(255,0,0,128),-1) cv2.addWeighted(overlay, opacity, frame, 1 - opacity, 0, frame) return stageChange
def sum_rgb(src): b,g,r = cv2.split(src) s = np.zeros(src.shape,dtype=np.uint8) dst = np.zeros(src.shape, dtype=np.uint8) s = cv2.addWeighted(r, 1./3., g, 1./3., 0.0) s = cv2.addWeighted(s, 2./3., b, 1./3., 0.0) ret, dst = cv2.threshold(s, 100, 100, cv2.THRESH_TRUNC) return dst
def example4_sharpen_explicit (imgChannel=None): if (imgChannel==None): imgChannel = cv2.imread ('tone_output_1.png') if (imgChannel==None): print 'Image file data not found and read; Abort'; return None, None, None tmp = cv2.GaussianBlur(imgChannel, (0,0), 0.01) imgChannel0 = cv2.addWeighted(imgChannel, 1.5, tmp, -0.5, 0) tmp = cv2.GaussianBlur(imgChannel, (0,0), 1) imgChannel1 = cv2.addWeighted(imgChannel, 1.5, tmp, -0.5, 0) tmp = cv2.GaussianBlur(imgChannel, (0,0), 3) imgChannel2 = cv2.addWeighted(imgChannel, 1.5, tmp, -0.5, 0) return imgChannel0, imgChannel1, imgChannel2
def kmPyramidSummation(pyr1, pyr2, pyr3, pyr4): pyr2R = cv2.resize(pyr2, (pyr1.shape[1], pyr1.shape[0]), interpolation=cv2.INTER_LINEAR) pyr3R = cv2.resize(pyr3, (pyr1.shape[1], pyr1.shape[0]), interpolation=cv2.INTER_LINEAR) pyr4R = cv2.resize(pyr4, (pyr1.shape[1], pyr1.shape[0]), interpolation=cv2.INTER_LINEAR) tempPyr = cv2.addWeighted(pyr4R, 0.25, pyr3R, 0.25, 0.0) tempPyr = cv2.addWeighted(pyr2R, 0.25, tempPyr, 1.0, 0.0) tempPyr = cv2.addWeighted(pyr1, 0.25, tempPyr, 1.0, 0.0) return tempPyr.astype(np.uint8)
def draw_contours(img, contours, hierarchy, color, opacity=1, offset = (0,0)): """ Draw contours on an image with a given opacity """ if not contours: return overlay = img.copy() #cv2.drawContours(overlay,contours,-1, color,-1) cv2.drawContours(overlay,contours,-1, color,-1, 8, hierarchy, 1, offset) cv2.addWeighted(overlay, opacity, img, 1 - opacity, 0, img)
def weighted_img(img, initial_img, a=0.8, b=1, r=0.0): return cv2.addWeighted(initial_img, a, img, b, r)
pic_list = [] for item in os.listdir('picture'): if item.endswith('.jpg') or item.endswith('.JPG'): pic_list.append(item) total = len(pic_list) x = 0 y = 0 for i in range(int(x_index*y_index)): # print(f'目前进度{i}/{x_index*y_index}') # print('--------------------第 %s 个---------------------------' %i) # print(i) # print(total) # print(i%total) temp = Image.open('picture\\' + pic_list[i%total]).resize((unit_size,unit_size), Image.ANTIALIAS) temp_img.paste(temp, (x*unit_size, y*unit_size)) x += 1 if x == x_index: x = 0 y += 1 print('素材合成完成') temp_img.save('temp.jpg', quality=100) src_temp = cv2.imread('temp.jpg') result = cv2.addWeighted(src_main, 0.6, src_temp, 0.4, 0) cv2.imwrite('result.jpg', result)
cv2.fillPoly(mask, polygons, 255) masked_image = cv2.bitwise_and(image, mask) # return masked_image ##image = cv2.imread('test_image.jpg') ##lane_image = np.copy(image) ##canny_image = canny(lane_image) ##cropped_image = region_of_interest(canny_image) ##lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5) ##averaged_lines = average_slope_intercept(lane_image, lines) ##line_image = display_lines(lane_image, averaged_lines) ##combo_image = cv2.addWeighted(lane_image, 0.8, line_image, 1, 1) ##cv2.imshow('result', combo_image) ##cv2.waitKey(0) cap = cv2.VideoCapture("test2.mp4") while(cap.isOpened()): _, frame = cap.read() canny_image = canny(frame) cropped_image = region_of_interest(canny_image) lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5) averaged_lines = average_slope_intercept(frame, lines) line_image = display_lines(frame, averaged_lines) combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1) cv2.imshow('result', combo_image) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def enhance(self, hsvImg): gauss = cv2.GaussianBlur(hsvImg, ksize=(5, 5), sigmaX=9) sum = cv2.addWeighted(hsvImg, 1.5, gauss, -0.6, 0) enhancedImg = cv2.medianBlur(sum, 3) enhancedImg = cv2.GaussianBlur(enhancedImg, ksize=(5, 5), sigmaX=2) return enhancedImg
def vis_keypoints(self, img, kps, kp_thresh=0.4, alpha=1): # Convert from plt 0-1 RGBA colors to 0-255 BGR colors for opencv. cmap = plt.get_cmap('rainbow') colors = [cmap(i) for i in np.linspace(0, 1, len(self.kps_lines) + 2)] colors = [(c[2] * 255, c[1] * 255, c[0] * 255) for c in colors] # Perform the drawing on a copy of the image, to allow for blending. kp_mask = np.copy(img) # Draw mid shoulder / mid hip first for better visualization. mid_shoulder = (kps[:2, 5] + kps[:2, 6]) / 2.0 sc_mid_shoulder = np.minimum(kps[2, 5], kps[2, 6]) mid_hip = (kps[:2, 11] + kps[:2, 12]) / 2.0 sc_mid_hip = np.minimum(kps[2, 11], kps[2, 12]) nose_idx = 0 if sc_mid_shoulder > kp_thresh and kps[2, nose_idx] > kp_thresh: cv2.line(kp_mask, tuple(mid_shoulder.astype(np.int32)), tuple(kps[:2, nose_idx].astype(np.int32)), color=colors[len(self.kps_lines)], thickness=2, lineType=cv2.LINE_AA) if sc_mid_shoulder > kp_thresh and sc_mid_hip > kp_thresh: cv2.line(kp_mask, tuple(mid_shoulder.astype(np.int32)), tuple(mid_hip.astype(np.int32)), color=colors[len(self.kps_lines) + 1], thickness=2, lineType=cv2.LINE_AA) # Draw the keypoints. for l in range(len(self.kps_lines)): i1 = self.kps_lines[l][0] i2 = self.kps_lines[l][1] p1 = kps[0, i1].astype(np.int32), kps[1, i1].astype(np.int32) p2 = kps[0, i2].astype(np.int32), kps[1, i2].astype(np.int32) if kps[2, i1] > kp_thresh and kps[2, i2] > kp_thresh: cv2.line(kp_mask, p1, p2, color=colors[l], thickness=2, lineType=cv2.LINE_AA) if kps[2, i1] > kp_thresh: cv2.circle(kp_mask, p1, radius=3, color=colors[l], thickness=-1, lineType=cv2.LINE_AA) if kps[2, i2] > kp_thresh: cv2.circle(kp_mask, p2, radius=3, color=colors[l], thickness=-1, lineType=cv2.LINE_AA) # Blend the keypoints. return cv2.addWeighted(img, 1.0 - alpha, kp_mask, alpha, 0)
aligned_frames = align.process(frames) depth_frame = aligned_frames.get_depth_frame() color_frame = frames.get_color_frame() if not depth_frame or not color_frame: continue # Convert images to numpy arrays depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) cv2.imwrite(os.path.join(os.path.join(path, 'd'), str(i) + '.pgm'), depth_image) cv2.imwrite(os.path.join(os.path.join(path, 'rgb'), str(i) + '.ppm'), color_image) colorizer = rs.colorizer() depth_image_colorized = np.asanyarray( colorizer.colorize(depth_frame).get_data()) # Show images combined = cv2.addWeighted(color_image, 0.5, depth_image_colorized, 0.5, 0) cv2.namedWindow('RGB-D', cv2.WINDOW_AUTOSIZE) cv2.imshow('RGB-D', combined) cv2.waitKey(1) except KeyboardInterrupt: # Stop streaming pipeline.stop()
draw_bounding_box(face_coordinates, rgb_image, color) draw_text(face_coordinates, rgb_image, emotion_mode, color, 0, -45, 1, 1) waterend = time.time() eyestrainend = time.time() if (waterend - waterstart > 3600): print("DRINK WATER") waterstart = time.time() if (eyestrainend - eyestrainstart > 7200): print("TAKE A BREAK") eyestrainstart = time.time() checkminute = time.time() if checkminute - minutetimer > 60: minutetimer = time.time() if blinkcount < 15: print("BLINK MORE") blinkcount = 0 bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) added_image = cv2.addWeighted(img, 0.4, bgr_image, 0.5, 0) cv2.imshow('window_frame', added_image) # cv2.imshow('eyes', img) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()
from PIL import Image import cv2 import numpy as np from google.colab.patches import cv2_imshow #upload the landuse image of Sangli district fg = Image.open("gdrive/My Drive/landuse.JPG") # converting the color mode of the second image(for Wards of Sangli) to match the first image, while opening the second image bg = Image.open("gdrive/My Drive/wards.JPG").convert(fg.mode) # resizing the second image to the same dimensions as the first one bg = bg.resize(fg.size) # creating an numpy array off both the image objects, for using in addWeighted() bg = np.array(bg) fg = np.array(fg) #add how much enhanced the respective images should be in the blended image img = cv2.addWeighted(fg, 0.4, bg, 0.6, 0) cv2_imshow(fg) cv2_imshow(bg) cv2_imshow(img) cv2.waitKey(0) cv2.destroyAllWindows() #boundary isn't properly matching """Doing this procedure again but doing translation(or image shifting) in the wards image""" fg = Image.open("gdrive/My Drive/landuse.JPG")
def vis_keypoints(img, kps, kp_thresh=2, alpha=0.7): """Visualizes keypoints (adapted from vis_one_image). kps has shape (4, #keypoints) where 4 rows are (x, y, logit, prob). """ dataset_keypoints, _ = keypoint_utils.get_keypoints() kp_lines = kp_connections(dataset_keypoints) # Convert from plt 0-1 RGBA colors to 0-255 BGR colors for opencv. cmap = plt.get_cmap('rainbow') colors = [cmap(i) for i in np.linspace(0, 1, len(kp_lines) + 2)] colors = [(c[2] * 255, c[1] * 255, c[0] * 255) for c in colors] # Perform the drawing on a copy of the image, to allow for blending. kp_mask = np.copy(img) # Draw mid shoulder / mid hip first for better visualization. mid_shoulder = (kps[:2, dataset_keypoints.index('right_shoulder')] + kps[:2, dataset_keypoints.index('left_shoulder')]) / 2.0 sc_mid_shoulder = np.minimum( kps[2, dataset_keypoints.index('right_shoulder')], kps[2, dataset_keypoints.index('left_shoulder')]) mid_hip = (kps[:2, dataset_keypoints.index('right_hip')] + kps[:2, dataset_keypoints.index('left_hip')]) / 2.0 sc_mid_hip = np.minimum(kps[2, dataset_keypoints.index('right_hip')], kps[2, dataset_keypoints.index('left_hip')]) nose_idx = dataset_keypoints.index('nose') if sc_mid_shoulder > kp_thresh and kps[2, nose_idx] > kp_thresh: cv2.line(kp_mask, tuple(mid_shoulder), tuple(kps[:2, nose_idx]), color=colors[len(kp_lines)], thickness=2, lineType=cv2.LINE_AA) if sc_mid_shoulder > kp_thresh and sc_mid_hip > kp_thresh: cv2.line(kp_mask, tuple(mid_shoulder), tuple(mid_hip), color=colors[len(kp_lines) + 1], thickness=2, lineType=cv2.LINE_AA) # Draw the keypoints. for l in range(len(kp_lines)): i1 = kp_lines[l][0] i2 = kp_lines[l][1] p1 = kps[0, i1], kps[1, i1] p2 = kps[0, i2], kps[1, i2] if kps[2, i1] > kp_thresh and kps[2, i2] > kp_thresh: cv2.line(kp_mask, p1, p2, color=colors[l], thickness=2, lineType=cv2.LINE_AA) if kps[2, i1] > kp_thresh: cv2.circle(kp_mask, p1, radius=3, color=colors[l], thickness=-1, lineType=cv2.LINE_AA) if kps[2, i2] > kp_thresh: cv2.circle(kp_mask, p2, radius=3, color=colors[l], thickness=-1, lineType=cv2.LINE_AA) # Blend the keypoints. return cv2.addWeighted(img, 1.0 - alpha, kp_mask, alpha, 0)
# demo_weightedsum.py import cv2 import numpy as np img1 = cv2.imread('cat.jpg', 1) img2 = cv2.imread('dog.jpg', 1) print(img1.shape) print(img2.shape) img2 = img2[0:img1.shape[0], :, :] print(img2.shape) # quit() cv2.imshow('Weighted image', img1) print('Switch to image window. Then press any key to continue.') cv2.waitKey(0) # Wait for key press N = 100 for i in range(N): y = cv2.addWeighted(img1, 1 - i / (N - 1), img2, i / (N - 1), 0) # y : weighted sum of two images cv2.imshow('Weighted image', y) cv2.waitKey(int(2000.0 / N)) # 1 second for total loop cv2.destroyAllWindows()
light = cv2.VideoCapture('light.mp4') while origin.isOpened() & sky.isOpened() & light.isOpened(): # 获取每一帧 ret1, origin_frame = origin.read() ret2, sky_frame = sky.read() ret3, light_frame = light.read() # 原视频画面转换到HSV hsv = cv2.cvtColor(origin_frame, cv2.COLOR_BGR2HSV) # 设定绿色的阈值 lower_green = np.array([50, 43, 46]) upper_green = np.array([70, 255, 255]) # 根据阈值构建掩模 mask = cv2.inRange(hsv, lower_green, upper_green) # 对原图像和掩模进行位运算,去除绿色背景 res = cv2.bitwise_and(origin_frame, origin_frame, mask=~mask) # 为使前景覆盖背景而不是叠加在背景上,先将背景和前景的重合区域除去(置为黑色) dst = cv2.addWeighted(sky_frame, 1, res, -20, 0) # 视频合成 dst1 = cv2.addWeighted(dst, 1, res, 1, 0) dst2 = cv2.addWeighted(dst1, 1, light_frame, 1, 0) # 显示视频每一帧(包括掩模和原视频) cv2.imshow('frame', dst) cv2.imshow('res', res) cv2.imshow('mask', ~mask) cv2.imshow('output', dst2) # ESC键退出 if cv2.waitKey(10) & 0xFF == 27: break # 关闭窗口 cv2.destroyAllWindows()
import cv2 import numpy as np image = cv2.imread('1.jpg') cv2.namedWindow('frame', 0) cv2.resizeWindow('frame', 640, 480) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) x = cv2.Sobel(src=image, ddepth=cv2.CV_16S, dx=1, dy=0) y = cv2.Sobel(src=image, ddepth=cv2.CV_16S, dx=0, dy=1) absX = cv2.convertScaleAbs(x) # 转回uint8 absY = cv2.convertScaleAbs(y) dst = cv2.addWeighted(absX, 0.5, absY, 0.5, 0) contours, hierarchy = cv2.findContours(dst, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cv2.drawContours(image=image, contours=contours, contourIdx=-1, color=255, thickness=3) cv2.imshow('frame', dst) if cv2.waitKey(0) & 0xFF == ord('q'): cv2.destroyAllWindows()
def pose2d_detectHuman(net, img, height_size=256, track=1, smooth=1, bVis=True): stride = 8 upsample_ratio = 4 num_keypoints = Pose.num_kpts previous_poses = [] delay = 33 if True: # for img in image_provider: orig_img = img.copy() heatmaps, pafs, scale, pad = infer_fast( net, img, height_size, stride, upsample_ratio, cpu=not torch.cuda.is_available()) total_keypoints_num = 0 all_keypoints_by_type = [] for kpt_idx in range(num_keypoints): # 19th for bg total_keypoints_num += extract_keypoints(heatmaps[:, :, kpt_idx], all_keypoints_by_type, total_keypoints_num) pose_entries, all_keypoints = group_keypoints(all_keypoints_by_type, pafs, demo=True) for kpt_id in range(all_keypoints.shape[0]): all_keypoints[kpt_id, 0] = (all_keypoints[kpt_id, 0] * stride / upsample_ratio - pad[1]) / scale all_keypoints[kpt_id, 1] = (all_keypoints[kpt_id, 1] * stride / upsample_ratio - pad[0]) / scale current_poses = [] for n in range(len(pose_entries)): if len(pose_entries[n]) == 0: continue pose_keypoints = np.ones((num_keypoints, 2), dtype=np.int32) * -1 for kpt_id in range(num_keypoints): if pose_entries[n][kpt_id] != -1.0: # keypoint was found pose_keypoints[kpt_id, 0] = int( all_keypoints[int(pose_entries[n][kpt_id]), 0]) pose_keypoints[kpt_id, 1] = int( all_keypoints[int(pose_entries[n][kpt_id]), 1]) pose = Pose(pose_keypoints, pose_entries[n][18]) current_poses.append(pose) if bVis: if track: track_poses(previous_poses, current_poses, smooth=smooth) previous_poses = current_poses for pose in current_poses: pose.draw(img) img = cv2.addWeighted(orig_img, 0.6, img, 0.4, 0) for pose in current_poses: cv2.rectangle( img, (pose.bbox[0], pose.bbox[1]), (pose.bbox[0] + pose.bbox[2], pose.bbox[1] + pose.bbox[3]), (0, 255, 0)) if track: cv2.putText(img, 'id: {}'.format(pose.id), (pose.bbox[0], pose.bbox[1] - 16), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255)) cv2.imshow('Lightweight Human Pose Estimation Python Demo', img) key = cv2.waitKey(delay) if key == 27: # esc return elif key == 112: # 'p' if delay == 33: delay = 0 else: delay = 33 return current_poses
def draw_multilabel_segmentation_predictions( input: dict, output: dict, class_colors: List, mode="side-by-side", image_key="features", image_id_key="image_id", targets_key="targets", outputs_key="logits", mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_images=None, targets_threshold=0.5, logits_threshold=0, image_format: Union[str, Callable] = "rgb", ) -> List[np.ndarray]: """ Render visualization of model's prediction for binary segmentation problem. This function draws a color-coded overlay on top of the image, with color codes meaning: - green: True positives - red: False-negatives - yellow: False-positives :param input: Input batch (model's input batch) :param output: Output batch (model predictions) :param class_colors: :param mode: :param image_key: Key for getting image :param image_id_key: Key for getting image id/fname :param targets_key: Key for getting ground-truth mask :param outputs_key: Key for getting model logits for predicted mask :param mean: Mean vector user during normalization :param std: Std vector user during normalization :param max_images: Maximum number of images to visualize from batch (If you have huge batch, saving hundreds of images may make TensorBoard slow) :param image_format: Source format of the image tensor to conver to RGB representation. Can be string ("gray", "rgb", "brg") or function `convert(np.ndarray)->nd.ndarray`. :return: List of images """ assert mode in {"overlay", "side-by-side"} images = [] num_samples = len(input[image_key]) if max_images is not None: num_samples = min(num_samples, max_images) for i in range(num_samples): image = rgb_image_from_tensor(input[image_key][i], mean, std) if image_format == "rgb": pass elif image_format == "bgr": image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) elif image_format == "gray": image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) elif isinstance(image_format, callable): image = image_format(image) target = to_numpy(input[targets_key][i]) > targets_threshold logits = to_numpy(output[outputs_key][i]) > logits_threshold if mode == "overlay": overlay = image.copy() for class_index, class_color in enumerate(class_colors): overlay[logits[class_index], :] = class_color overlay = cv2.addWeighted(image, 0.5, overlay, 0.5, 0, dtype=cv2.CV_8U) elif mode == "side-by-side": true_mask = image.copy() for class_index, class_color in enumerate(class_colors): true_mask[target[class_index], :] = class_color pred_mask = image.copy() for class_index, class_color in enumerate(class_colors): pred_mask[logits[class_index], :] = class_color true_mask = cv2.addWeighted(image, 0.5, true_mask, 0.5, 0, dtype=cv2.CV_8U) pred_mask = cv2.addWeighted(image, 0.5, pred_mask, 0.5, 0, dtype=cv2.CV_8U) overlay = np.hstack((true_mask, pred_mask)) else: raise ValueError(mode) if image_id_key is not None and image_id_key in input: image_id = input[image_id_key][i] cv2.putText(overlay, str(image_id), (10, 15), cv2.FONT_HERSHEY_PLAIN, 1, (250, 250, 250)) images.append(overlay) return images
# print 'face found' cv2.rectangle(img, (x, y), (x + w, y + h), 0, 2) roi_face = gray[y:y + h, x:x + w] roi_face_color = img[y:y + h, x:x + w] eyes = eye_cascade.detectMultiScale(roi_face, 1.3, 5) for (ex, ey, ew, eh) in eyes: counter += 1 cv2.rectangle(roi_face_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) # print "eye " + str(ex) + " " + str(ey) # roi_eye = roi_face[int(1.2*ey):int(0.8*(ey+eh)), int(1.2*ex):int(0.8*(ex+ew))] roi_eye = roi_face[ey:ey + eh, ex:ex + ew] center = 0 roi_eye = cv2.GaussianBlur(roi_eye, (3, 3), 0) roi_eye = cv2.addWeighted(roi_eye, 1.5, roi_eye, -0.5, 0) roi_eye_canny = cv2.Canny(roi_eye, 100, 200) cv2.imwrite('./data/canny' + str(counter) + '.png', roi_eye_canny) laplacian = cv2.Laplacian(roi_eye, cv2.CV_64F) cv2.imwrite('./data/lapla' + str(counter) + '.png', laplacian) # res = cv2.resize(roi_eye,(int(ew/2), int(eh/2)), interpolation = cv2.INTER_AREA) roi_eyex = cv2.Sobel(roi_eye, cv2.CV_64F, 1, 0, ksize=3) roi_eyey = cv2.Sobel(roi_eye, cv2.CV_64F, 0, 1, ksize=3) roi_eyex = np.absolute(roi_eyex) roi_eyey = np.absolute(roi_eyey) roi_eyex = np.uint8(roi_eyex) roi_eyey = np.uint8(roi_eyey) # sobelx64f = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5) # abs_sobel64f = np.absolute(sobelx64f) # sobel_8u = np.uint8(abs_sobel64f)
def draw_binary_segmentation_predictions( input: dict, output: dict, image_key="features", image_id_key: Optional[str] = "image_id", targets_key="targets", outputs_key="logits", mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), max_images=None, targets_threshold=0.5, logits_threshold=0, image_format: Union[str, Callable] = "rgb", ) -> List[np.ndarray]: """ Render visualization of model's prediction for binary segmentation problem. This function draws a color-coded overlay on top of the image, with color codes meaning: - green: True positives - red: False-negatives - yellow: False-positives :param input: Input batch (model's input batch) :param output: Output batch (model predictions) :param image_key: Key for getting image :param image_id_key: Key for getting image id/fname :param targets_key: Key for getting ground-truth mask :param outputs_key: Key for getting model logits for predicted mask :param mean: Mean vector user during normalization :param std: Std vector user during normalization :param max_images: Maximum number of images to visualize from batch (If you have huge batch, saving hundreds of images may make TensorBoard slow) :param targets_threshold: Threshold to convert target values to binary. Default value 0.5 is safe for both smoothed and hard labels. :param logits_threshold: Threshold to convert model predictions (raw logits) values to binary. Default value 0.0 is equivalent to 0.5 after applying sigmoid activation :param image_format: Source format of the image tensor to conver to RGB representation. Can be string ("gray", "rgb", "brg") or function `convert(np.ndarray)->nd.ndarray`. :return: List of images """ images = [] num_samples = len(input[image_key]) if max_images is not None: num_samples = min(num_samples, max_images) assert output[outputs_key].size(1) == 1, "Mask must be single-channel tensor of shape [Nx1xHxW]" for i in range(num_samples): image = rgb_image_from_tensor(input[image_key][i], mean, std) if image_format == "rgb": pass elif image_format == "bgr": image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) elif image_format == "gray": image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) elif isinstance(image_format, callable): image = image_format(image) target = to_numpy(input[targets_key][i]).squeeze(0) logits = to_numpy(output[outputs_key][i]).squeeze(0) overlay = image.copy() true_mask = target > targets_threshold pred_mask = logits > logits_threshold overlay[true_mask & pred_mask] = np.array( [0, 250, 0], dtype=overlay.dtype ) # Correct predictions (Hits) painted with green overlay[true_mask & ~pred_mask] = np.array([250, 0, 0], dtype=overlay.dtype) # Misses painted with red overlay[~true_mask & pred_mask] = np.array( [250, 250, 0], dtype=overlay.dtype ) # False alarm painted with yellow overlay = cv2.addWeighted(image, 0.5, overlay, 0.5, 0, dtype=cv2.CV_8U) if image_id_key is not None and image_id_key in input: image_id = input[image_id_key][i] cv2.putText(overlay, str(image_id), (10, 15), cv2.FONT_HERSHEY_PLAIN, 1, (250, 250, 250)) images.append(overlay) return images
cv2.imshow('img', img) cv2.waitKey() exit(0) hist_array = gray_hist = cv2.calcHist([img], [0], None, [256], [0.0, 255.0]) # clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # dst = clahe.apply(gray) # img = img - 70 mv = cv2.split(img) sobelx = cv2.Sobel(mv[0], cv2.CV_16S, 1, 0, ksize=3) sobely = cv2.Sobel(mv[0], cv2.CV_16S, 0, 1, ksize=3) sobely = cv2.convertScaleAbs(sobely) sobelx = cv2.convertScaleAbs(sobelx) dst = cv2.addWeighted(sobelx, 0.5, sobely, 0.5, 0) dd = cv2.Laplacian(mv[0], cv2.CV_16S) dd = cv2.convertScaleAbs(dd) img2 = cv2.adaptiveThreshold(dst, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) cv2.imshow('1', dst) cv2.waitKey() exit(0) # img = cv2.medianBlur(img, 9) 中值滤波 # img = cv2.bilateralFilter(img, 25, 50, 25/2) 双边滤波,效率太低,效果也不算好 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) mv = cv2.split(img) imglap = cv2.Laplacian(gray, cv2.CV_64F) img2 = cv2.adaptiveThreshold(mv[0], 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) img3 = cv2.medianBlur(img2, 9)
def search_around_poly(binary_warped): # HYPERPARAMETER # Choose the width of the margin around the previous polynomial to search # The quiz grader expects 100 here, but feel free to tune on your own! margin = 100 # Grab activated pixels nonzero = binary_warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) ### TO-DO: Set the area of search based on activated x-values ### ### within the +/- margin of our polynomial function ### ### Hint: consider the window areas for the similarly named variables ### ### in the previous quiz, but change the windows to our new search area ### left_lane_inds = ((nonzerox > (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] - margin)) & (nonzerox < (left_fit[0]*(nonzeroy**2) + left_fit[1]*nonzeroy + left_fit[2] + margin))).nonzero()[0] right_lane_inds = ((nonzerox > (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] - margin)) & (nonzerox < (right_fit[0]*(nonzeroy**2) + right_fit[1]*nonzeroy + right_fit[2] + margin))).nonzero()[0] # Again, extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit new polynomials left_fitx, right_fitx, ploty = fit_poly(binary_warped.shape, leftx, lefty, rightx, righty) ## Visualization ## # Create an image to draw on and an image to show the selection window out_img = np.dstack((binary_warped, binary_warped, binary_warped))*255 window_img = np.zeros_like(out_img) # Color in left and right line pixels out_img[nonzeroy[left_lane_inds], nonzerox[left_lane_inds]] = [255, 0, 0] out_img[nonzeroy[right_lane_inds], nonzerox[right_lane_inds]] = [0, 0, 255] # Generate a polygon to illustrate the search window area # And recast the x and y points into usable format for cv2.fillPoly() left_line_window1 = np.array([np.transpose(np.vstack([left_fitx-margin, ploty]))]) print left_line_window1 left_line_window2 = np.array([np.flipud(np.transpose(np.vstack([left_fitx+margin, ploty])))]) print left_line_window2 left_line_pts = np.hstack((left_line_window1, left_line_window2)) print left_line_pts right_line_window1 = np.array([np.transpose(np.vstack([right_fitx-margin, ploty]))]) right_line_window2 = np.array([np.flipud(np.transpose(np.vstack([right_fitx+margin, ploty])))]) right_line_pts = np.hstack((right_line_window1, right_line_window2)) # Draw the lane onto the warped blank image cv2.fillPoly(window_img, np.int_([left_line_pts]), (0,255, 0)) cv2.fillPoly(window_img, np.int_([right_line_pts]), (0,255, 0)) result = cv2.addWeighted(out_img, 1, window_img, 0.3, 0) # Plot the polynomial lines onto the image plt.plot(left_fitx, ploty, color='yellow') plt.plot(right_fitx, ploty, color='yellow') ## End visualization steps ## return result
def box_extraction(img_for_box_extraction_path): img = cv2.imread(img_for_box_extraction_path, 0) # Read the image height, width = img.shape[:2] print(height, width) (thresh, img_bin) = cv2.threshold( img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # Thresholding the image img_bin = 255 - img_bin # Invert the image # Defining a kernel length kernel_length = np.array(img).shape[1] // 40 # A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image. verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length)) # A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image. hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) # A kernel of (3 X 3) ones. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # Morphological operation to detect verticle lines from an image img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3) verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3) cv2.imwrite("verticle_lines.jpg", verticle_lines_img) # Morphological operation to detect horizontal lines from an image img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=3) horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=3) cv2.imwrite("horizontal_lines.jpg", horizontal_lines_img) # Weighting parameters, this will decide the quantity of an image to be added to make a new image. alpha = 0.5 beta = 1.0 - alpha # This function helps to add two image with specific weight parameter to get a third image as summation of two image. img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0) img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2) (thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # For Debugging # Enable this line to see verticle and horizontal lines in the image which is used to find boxes cv2.imwrite("img_final_bin.jpg", img_final_bin) # Find contours for image, which will detect all the boxes contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Sort all the contours by top to bottom. (contours, boundingBoxes) = sort_contours(contours, method="top-to-bottom") idx = 0 for c in contours: # Returns the location and width,height for every contour x, y, w, h = cv2.boundingRect(c) # If the box height is greater then 20, widht is >80, then only save it as a box in "cropped/" folder. if (w > width / 2 and h > 250) and ( w > 3 * h) and idx == 0: #if (w > width/2.3 and h > 400) and w > 3*h: idx += 1 yH = y new_img = img[y:y + h, x:x + w] x1 = x x2 = x + w y2 = y + h height2 = h table1 = new_img table2 = img[y2:y2 + height2 - 35, x1:x2] return table1, table2, yH
def vis_frame(self, frame, im_res, format='coco'): ''' frame: frame image im_res: im_res of predictions format: coco or mpii return rendered image ''' if format == 'coco': l_pair = [ (0, 1), (0, 2), (1, 3), (2, 4), # Head (5, 6), (5, 7), (7, 9), (6, 8), (8, 10), (17, 11), (17, 12), # Body (11, 13), (12, 14), (13, 15), (14, 16) ] p_color = [ (0, 255, 255), (0, 191, 255), (0, 255, 102), (0, 77, 255), (0, 255, 0), # Nose, LEye, REye, LEar, REar (77, 255, 255), (77, 255, 204), (77, 204, 255), (191, 255, 77), (77, 191, 255), (191, 255, 77), # LShoulder, RShoulder, LElbow, RElbow, LWrist, RWrist (204, 77, 255), (77, 255, 204), (191, 77, 255), (77, 255, 191), (127, 77, 255), (77, 255, 127), (0, 255, 255) ] # LHip, RHip, LKnee, Rknee, LAnkle, RAnkle, Neck line_color = [(0, 215, 255), (0, 255, 204), (0, 134, 255), (0, 255, 50), (77, 255, 222), (77, 196, 255), (77, 135, 255), (191, 255, 77), (77, 255, 77), (77, 222, 255), (255, 156, 127), (0, 127, 255), (255, 127, 77), (0, 77, 255), (255, 77, 36)] elif format == 'mpii': l_pair = [(8, 9), (11, 12), (11, 10), (2, 1), (1, 0), (13, 14), (14, 15), (3, 4), (4, 5), (8, 7), (7, 6), (6, 2), (6, 3), (8, 12), (8, 13)] p_color = [ self.PURPLE, self.BLUE, self.BLUE, self.RED, self.RED, self.BLUE, self.BLUE, self.RED, self.RED, self.PURPLE, self.PURPLE, self.PURPLE, self.RED, self.RED, self.BLUE, self.BLUE ] line_color = [ self.PURPLE, self.BLUE, self.BLUE, self.RED, self.RED, self.BLUE, self.BLUE, self.RED, self.RED, self.PURPLE, self.PURPLE, self.RED, self.RED, self.BLUE, self.BLUE ] else: raise NotImplementedError im_name = im_res['imgname'].split('/')[-1] img = frame.asnumpy() height, width = frame.shape[:2] img = cv2.resize(img, (int(width / 2), int(height / 2))) for human in im_res['result']: part_line = {} kp_preds = human['keypoints'] kp_scores = human['kp_score'] kp_preds = np.concatenate( (kp_preds, ((kp_preds[5, :] + kp_preds[6, :]) / 2, 0)[0][np.newaxis, :]), axis=0) kp_scores = np.concatenate( (kp_scores, ((kp_scores[5, :] + kp_scores[6, :]) / 2, 0)[0][np.newaxis, :]), axis=0) # Draw keypoints for n in range(kp_scores.shape[0]): if kp_scores[n] <= 0.05: continue cor_x, cor_y = int(kp_preds[n, 0]), int(kp_preds[n, 1]) part_line[n] = (int(cor_x / 2), int(cor_y / 2)) bg = img.copy() cv2.circle(bg, (int(cor_x / 2), int(cor_y / 2)), 2, p_color[n], -1) # Now create a mask of logo and create its inverse mask also transparency = max(0, min(1, kp_scores[n])) img = cv2.addWeighted(bg, transparency, img, 1 - transparency, 0) # Draw limbs for i, (start_p, end_p) in enumerate(l_pair): if start_p in part_line and end_p in part_line: start_xy = part_line[start_p] end_xy = part_line[end_p] bg = img.copy() X = (start_xy[0], end_xy[0]) Y = (start_xy[1], end_xy[1]) mX = np.mean(X) mY = np.mean(Y) length = ((Y[0] - Y[1])**2 + (X[0] - X[1])**2)**0.5 angle = math.degrees(math.atan2(Y[0] - Y[1], X[0] - X[1])) stickwidth = (kp_scores[start_p] + kp_scores[end_p]) + 1 polygon = cv2.ellipse2Poly((int(mX), int(mY)), (int(length / 2), stickwidth), int(angle), 0, 360, 1) cv2.fillConvexPoly(bg, polygon, line_color[i]) # cv2.line(bg, start_xy, end_xy, line_color[i], (2 * (kp_scores[start_p] + kp_scores[end_p])) + 1) transparency = max( 0, min(1, 0.5 * (kp_scores[start_p] + kp_scores[end_p]))) img = cv2.addWeighted(bg, transparency, img, 1 - transparency, 0) img = cv2.resize(img, (width, height), interpolation=cv2.INTER_CUBIC) if opt.save_img: cv2.imwrite(os.path.join('examples/res', im_name), img)
def preprocess(img): """Find the box containing the digits and convert them to MNIST format.""" # Threshold image # SRC: https://medium.com/coinmonks/a-box-detection-algorithm-for-any-image-containing-boxes-756c15d7ed26 # noqa: E501 thresh, img_bin = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) img_bin = 255 - img_bin # Defining a kernel length kernel_length = np.array(img).shape[1] // 80 verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length)) horizont_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # Search for vertical and horizontal lines img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=3) verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3) img_temp2 = cv2.erode(img_bin, horizont_kernel, iterations=3) horizontal_lines_img = cv2.dilate(img_temp2, horizont_kernel, iterations=3) alpha = 0.5 beta = 1.0 - alpha img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0) img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=3) thresh, img_final_bin = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) img_final_bin = 255 - img_final_bin contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours, boundingBoxes = sort_contours(contours) sub_boxes = [] idx = 0 # get the individual images for c in contours: x, y, w, h = cv2.boundingRect(c) if (w > 20 and w < 75 and h > 35 and h < 125): idx += 1 new_img = img[y:y + h, x:x + w] # convert to MNIST expected img # resize to 28x28,invert, and leave only 1 channel retval, thresh_gray = cv2.threshold(new_img, thresh=100, maxval=255, type=cv2.THRESH_BINARY_INV) # clean up noise and fill in any holes in the digit thresh_gray = cv2.medianBlur(thresh_gray, 3) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) thresh_gray = cv2.dilate(thresh_gray, kernel, iterations=1) thresh_gray = cv2.morphologyEx(thresh_gray, cv2.MORPH_CLOSE, kernel) contours, h = cv2.findContours(thresh_gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) if len(contours) < 1: continue contours = list( reversed(sorted(contours, key=lambda x: cv2.contourArea(x)))) # all noise has been removed, so we can combine the left over components c = contours[0] for c_index in range(1, len(contours)): c = np.vstack((c, contours[c_index])) x, y, w, h = cv2.boundingRect(c) thresh_gray = 255 - thresh_gray cut = thresh_gray[y:y + h, x:x + w] # redraw the digit at the center of the image cut_height = (y + h) - y cut_width = (x + w) - w if cut_height > cut_width: left = right = x // 2 top = bottom = 0 elif cut_width > cut_width: top = bottom = y // 2 left = right = 0 else: left = right = top = bottom = 0 border_size = 5 left += border_size right += border_size top += border_size bottom += border_size new_img = cv2.copyMakeBorder(cut, top, bottom, left, right, cv2.BORDER_CONSTANT, value=[255, 255, 255]) new_img = cv2.dilate(new_img, kernel) # downscale to expected size with border sf = 2 new_img = cv2.resize(new_img, (28, 28), fx=sf, fy=sf, interpolation=cv2.INTER_AREA) # force a border increase incase the resizing caused a digit to hit the edge border_size = 2 new_img = cv2.copyMakeBorder(new_img, border_size, border_size, border_size, border_size, cv2.BORDER_CONSTANT, value=[255, 255, 255]) new_img = cv2.resize(new_img, (28, 28), fx=sf, fy=sf, interpolation=cv2.INTER_AREA) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) new_img = cv2.dilate(new_img, kernel, iterations=1) new_img = 255 - new_img # normalize from 0-255 to 0-1 new_img = new_img / 255 # convert image to expected tensor (vector) new_img = np.expand_dims(new_img, axis=0) new_img = np.expand_dims(new_img, axis=0) new_img = new_img.astype(np.float32) sub_boxes.append(new_img) return sub_boxes
def __getitem__(self, index): img_path = list(self.truth.keys())[index] bboxes = np.array(self.truth.get(img_path), dtype=np.float) img_path = os.path.join(self.cfg.dataset_dir, img_path) use_mixup = self.cfg.mixup if random.randint(0, 1): use_mixup = 0 if use_mixup == 3: min_offset = 0.2 # 可变 cut_x = random.randint(int(self.cfg.w * min_offset), int(self.cfg.w * (1 - min_offset))) cut_y = random.randint(int(self.cfg.h * min_offset), int(self.cfg.h * (1 - min_offset))) r1, r2, r3, r4, r_scale = 0, 0, 0, 0, 0 dhue, dsat, dexp, flip, blur = 0, 0, 0, 0, 0 gaussian_noise = 0 out_img = np.zeros([self.cfg.h, self.cfg.w, 3]) out_bboxes = [] for i in range(use_mixup + 1): if i != 0: img_path = random.choice(list(self.truth.keys())) bboxes = np.array(self.truth.get(img_path), dtype=np.float) img_path = os.path.join(self.cfg.dataset_dir, img_path) img = cv2.imread(img_path) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) if img is None: continue oh, ow, oc = img.shape dh, dw, dc = np.array(np.array([oh, ow, oc]) * self.cfg.jitter, dtype=np.int) dhue = rand_uniform_strong(-self.cfg.hue, self.cfg.hue) dsat = rand_scale(self.cfg.saturation) dexp = rand_scale(self.cfg.exposure) pleft = random.randint(-dw, dw) pright = random.randint(-dw, dw) ptop = random.randint(-dh, dh) pbot = random.randint(-dh, dh) flip = random.randint(0, 1) if self.cfg.flip else 0 if (self.cfg.blur): tmp_blur = random.randint( 0, 2 ) # 0 - disable, 1 - blur background, 2 - blur the whole image if tmp_blur == 0: blur = 0 elif tmp_blur == 1: blur = 1 else: blur = self.cfg.blur if self.cfg.gaussian and random.randint(0, 1): gaussian_noise = self.cfg.gaussian else: gaussian_noise = 0 if self.cfg.letter_box: img_ar = ow / oh net_ar = self.cfg.w / self.cfg.h result_ar = img_ar / net_ar # print(" ow = %d, oh = %d, w = %d, h = %d, img_ar = %f, net_ar = %f, result_ar = %f \n", ow, oh, w, h, img_ar, net_ar, result_ar); if result_ar > 1: # sheight - should be increased oh_tmp = ow / net_ar delta_h = (oh_tmp - oh) / 2 ptop = ptop - delta_h pbot = pbot - delta_h # print(" result_ar = %f, oh_tmp = %f, delta_h = %d, ptop = %f, pbot = %f \n", result_ar, oh_tmp, delta_h, ptop, pbot); else: # swidth - should be increased ow_tmp = oh * net_ar delta_w = (ow_tmp - ow) / 2 pleft = pleft - delta_w pright = pright - delta_w # printf(" result_ar = %f, ow_tmp = %f, delta_w = %d, pleft = %f, pright = %f \n", result_ar, ow_tmp, delta_w, pleft, pright); swidth = ow - pleft - pright sheight = oh - ptop - pbot truth, min_w_h = fill_truth_detection(bboxes, self.cfg.boxes, self.cfg.classes, flip, pleft, ptop, swidth, sheight, self.cfg.w, self.cfg.h) if ( min_w_h / 8 ) < blur and blur > 1: # disable blur if one of the objects is too small blur = min_w_h / 8 ai = image_data_augmentation(img, self.cfg.w, self.cfg.h, pleft, ptop, swidth, sheight, flip, dhue, dsat, dexp, gaussian_noise, blur, truth) if use_mixup == 0: out_img = ai out_bboxes = truth if use_mixup == 1: if i == 0: old_img = ai.copy() old_truth = truth.copy() elif i == 1: out_img = cv2.addWeighted(ai, 0.5, old_img, 0.5) out_bboxes = np.concatenate([old_truth, truth], axis=0) elif use_mixup == 3: if flip: tmp = pleft pleft = pright pright = tmp left_shift = int( min(cut_x, max(0, (-int(pleft) * self.cfg.w / swidth)))) top_shift = int( min(cut_y, max(0, (-int(ptop) * self.cfg.h / sheight)))) right_shift = int( min((self.cfg.w - cut_x), max(0, (-int(pright) * self.cfg.w / swidth)))) bot_shift = int( min(self.cfg.h - cut_y, max(0, (-int(pbot) * self.cfg.h / sheight)))) out_img, out_bbox = blend_truth_mosaic( out_img, ai, truth.copy(), self.cfg.w, self.cfg.h, cut_x, cut_y, i, left_shift, right_shift, top_shift, bot_shift) out_bboxes.append(out_bbox) # print(img_path) if use_mixup == 3: out_bboxes = np.concatenate(out_bboxes, axis=0) out_bboxes1 = np.zeros([self.cfg.boxes, 5]) out_bboxes1[:min(out_bboxes.shape[0], self.cfg.boxes )] = out_bboxes[:min(out_bboxes.shape[0], self.cfg. boxes)] return out_img, out_bboxes1
import numpy as np import matplotlib.pyplot as plt def disp_im(im): fi=plt.figure(figsize=(12,10)) ax=fi.add_subplot(111) ax.imshow(im,cmap='gray') rain=cv2.imread('rainbow.jpeg') show_rain=cv2.cvtColor(rain,cv2.COLOR_BGR2RGB) horse=cv2.imread('horse.jpeg') show_horse=cv2.cvtColor(horse,cv2.COLOR_BGR2RGB) brick=cv2.imread('brick.jpeg') show_brick=cv2.cvtColor(brick,cv2.COLOR_BGR2RGB) hist_val=cv2.calcHist([rain],channels=[0],mask=None,histSize=[256],ranges=[0,256]) sobolx=cv2.Sobel(im,cv2.CV_64F,1,0,ksize=7) soboly=cv2.Sobel(im,cv2.CV_64F,0,1,ksize=7) blend=cv2.addWeighted(src1=sobolx,alpha=0.5,src2=soboly,beta=0.5,gamma=1) laplace=cv2.Laplacian(im,cv2.CV_64F) ret,thres=cv2.threshold(blend,45,255,cv2.THRESH_BINARY) #morphological operator kernel=np.ones((4,4),np.uint8) grad=cv2.morphologyEx(blend,cv2.MORPH_GRADIENT,kernel) disp_im(laplace)
# DISPLAYING ANSWERS utilis.showAnswers(imgWarpColored, myIndex, grading, ans) # DRAW DETECTED ANSWERS utilis.drawGrid(imgWarpColored) # DRAW GRID imgRawDrawings = np.zeros_like(imgWarpColored) # NEW BLANK IMAGE WITH WARP IMAGE SIZE utilis.showAnswers(imgRawDrawings, myIndex, grading, ans) # DRAW ON NEW IMAGE invMatrix = cv2.getPerspectiveTransform(pts2, pts1) # INVERSE TRANSFORMATION MATRIX imgInvWarp = cv2.warpPerspective(imgRawDrawings, invMatrix, (widthImg, heightImg)) # INV IMAGE WARP # DISPLAY GRADE imgRawGrade = np.zeros_like(imgGradeDisplay, np.uint8) # NEW BLANK IMAGE WITH GRADE AREA SIZE cv2.putText(imgRawGrade, str(int(score)) + "%", (70, 100), cv2.FONT_HERSHEY_COMPLEX, 3, (0, 85, 255), 3) # ADD THE GRADE TO NEW IMAGE invMatrixG = cv2.getPerspectiveTransform(ptsG2, ptsG1) # INVERSE TRANSFORMATION MATRIX imgInvGradeDisplay = cv2.warpPerspective(imgRawGrade, invMatrixG, (widthImg, heightImg)) # INV IMAGE WARP # SHOW ANSWERS AND GRADE ON FINAL IMAGE imgFinal = cv2.addWeighted(imgFinal, 1, imgInvWarp, 1, 0) imgFinal = cv2.addWeighted(imgFinal, 1, imgInvGradeDisplay, 1, 0) cv2.imshow("Final Result", imgFinal) cv2.waitKey(10000) imageList.append(imgFinal) labels.append(filename.partition('.')[0]) # SAVE IMAGE WHEN 's' key is pressed if cv2.waitKey(1) & 0xFF == ord('s'): cv2.imwrite("Scanned/myImage" + str(filename.partition('.')[0]) + ".jpg", imgFinal) cv2.waitKey(300)
def weighted_img(img, initial_img, α=1, β=1., λ=0.): # 두 이미지 operlap 하기 return cv2.addWeighted(initial_img, α, img, β, λ)
def process_image(image): with open('camera_cal.p', 'rb') as handle: data = pickle.load(handle) # Load the calibration mtx, dist = data['mtx'], data['dist'] # Create the perspective transform and its inverse image_size = (600, 1200) M, Minv = create_perspective_transforms(image_size) # Undistort the image undistorted = cv2.undistort(image, mtx, dist, None, mtx) color_binary, combined_binary = threshold_image(undistorted) # Generate the warped image warped = cv2.warpPerspective(combined_binary, M, image_size, flags=cv2.INTER_LINEAR) margin = 100 recent_line_count = 8 global recent_lines detected_lines = [ line_pair for line_pair in recent_lines if (line_pair[0].detected and line_pair[1].detected) ] if len(detected_lines) > 0: last_left_line, last_right_line = detected_lines[-1] else: last_left_line = None last_right_line = None left_line = Line(warped.shape[0]) left_line.inherit_fit(last_left_line, recent_line_count) right_line = Line(warped.shape[0]) right_line.inherit_fit(last_right_line, recent_line_count) # If we've already fitted lane line curves, use them to find the next curves if last_left_line is not None and last_right_line is not None: left_fit, right_fit = find_next_lane_line(warped, last_left_line.best_fit, last_right_line.best_fit, margin) if left_fit is not None and right_fit is not None: left_line.append_fit(left_fit, recent_line_count) right_line.append_fit(right_fit, recent_line_count) # else use convolution to detect the lane lines initially else: print("Resetting lane lines...") # How wide the search window will be window_width = 100 # How tall the search window will be - 720 pixel height produces 9 vertical layers window_height = 80 # How much to slide left and right for searching margin = 100 # Fraction of window width to use as a threshold for detecting a lane line threshold_pct = 0.01 # Now detect lane lines using convolution window_centroids = find_window_centroids(warped, window_height, window_height, margin, threshold_pct) detected = 255 * np.array( cv2.merge((warped, warped, warped)), np.uint8) # making the original road pixels 3 color channels # Arrays to receive the lane marking indexes nonzero = warped.nonzero() nonzeroy = np.array(nonzero[0]) nonzerox = np.array(nonzero[1]) left_lane_inds = [] right_lane_inds = [] # If we found any window centers if len(window_centroids) > 0: # Points used to draw all the left and right windows l_points = np.zeros_like(warped) r_points = np.zeros_like(warped) # Go through each level and draw the windows for level in range(0, len(window_centroids)): if window_centroids[level][0] is not None: l_mask, win_y_low, win_y_high, win_x_low, win_x_high = window_mask( window_width, window_height, warped, window_centroids[level][0], level) l_points[(l_points == 255) | ((l_mask == 1))] = 255 good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_x_low) & (nonzerox < win_x_high)).nonzero()[0] left_lane_inds.append(good_left_inds) if window_centroids[level][1] is not None: r_mask, win_y_low, win_y_high, win_x_low, win_x_high = window_mask( window_width, window_height, warped, window_centroids[level][1], level) r_points[(r_points == 255) | ((r_mask == 1))] = 255 good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) & (nonzerox >= win_x_low) & (nonzerox < win_x_high)).nonzero()[0] right_lane_inds.append(good_right_inds) if len(left_lane_inds) > 0 and len(right_lane_inds) > 0: left_lane_inds = np.concatenate(left_lane_inds) right_lane_inds = np.concatenate(right_lane_inds) # Extract left and right line pixel positions leftx = nonzerox[left_lane_inds] lefty = nonzeroy[left_lane_inds] rightx = nonzerox[right_lane_inds] righty = nonzeroy[right_lane_inds] # Fit a second order polynomial to each left_fit = np.polyfit(lefty, leftx, 2) right_fit = np.polyfit(righty, rightx, 2) left_line.append_fit(left_fit, recent_line_count) right_line.append_fit(right_fit, recent_line_count) recent_lines.append((left_line, right_line)) if len(recent_lines) > recent_line_count: recent_lines.pop(0) # Calculate the radius of curvature with respect to the bottom of the image # which is the car dashboard # Define conversions in x and y from pixels space to meters ym_per_pix = 30 / 720 # meters per pixel in y dimension xm_per_pix = 3.7 / 700 # meters per pixel in x dimension left_fit_cr, right_fit_cr = find_next_lane_line(warped, left_line.best_fit, right_line.best_fit, margin, ym_per_pix, xm_per_pix) if left_fit_cr is not None and right_fit_cr is not None: # Calculate the new radii of curvature y_eval = np.max(left_line.ploty) left_curverad = ( (1 + (2 * left_fit_cr[0] * y_eval * ym_per_pix + left_fit_cr[1])**2)** 1.5) / np.absolute(2 * left_fit_cr[0]) right_curverad = ( (1 + (2 * right_fit_cr[0] * y_eval * ym_per_pix + right_fit_cr[1])**2) **1.5) / np.absolute(2 * right_fit_cr[0]) roc = str((left_curverad + right_curverad) / 2.) else: roc = '---' if left_line.bestx is not None and right_line.bestx is not None: # Calculate the lane offset lane_offset = str( get_lane_offset(warped.shape[1], left_line.bestx, right_line.bestx, xm_per_pix)) else: lane_offset = '---' if left_line.bestx is not None and right_line.bestx is not None: # Create an image to draw the lines on warp_zero = np.zeros_like(warped).astype(np.uint8) color_warp = np.dstack((warp_zero, warp_zero, warp_zero)) # Recast the x and y points into usable format for cv2.fillPoly() pts_left = np.array( [np.transpose(np.vstack([left_line.bestx, left_line.ploty]))]) pts_right = np.array([ np.flipud( np.transpose(np.vstack([right_line.bestx, right_line.ploty]))) ]) pts = np.hstack((pts_left, pts_right)) # Draw the lane onto the warped blank image cv2.fillPoly(color_warp, np.int_([pts]), (0, 255, 0)) # Warp the blank back to original image space using inverse perspective matrix (Minv) newwarp = cv2.warpPerspective(color_warp, Minv, (image.shape[1], image.shape[0])) # Combine the result with the original image final = cv2.addWeighted(undistorted, 1, newwarp, 0.3, 0) else: final = undistorted font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(final, 'RoC: ' + roc + ' m', (50, 25), font, 1, (255, 255, 255)) cv2.putText(final, 'Offset: ' + lane_offset + ' m', (50, 50), font, 1, (255, 255, 255)) return final
def sharpen(img): blurred = cv2.GaussianBlur(img, (0, 0), 3) cv2.addWeighted(img, 1.5, blurred, -0.5, 0, blurred) return blurred
def unsharpMasking(image): gaussian = cv2.GaussianBlur(image, (9, 9), 10.0) return cv2.addWeighted( image, 1.5, gaussian, -0.5, 0, image ) #konacna = image(original)*1.5 - gaussian(zamucena)*0.5 + 0; k=0.5
exit_zone['exit_time'] = exit_time_str if exit_time - entry_time > 10: # req = requests.post(URL, json=exit_zone) # print(req) payload.append(exit_zone) print(exit_zone) zones.extend(trk.get_trail().get_current_zones()) stock_zone_in.push(zones) overlay = frame.copy() for z in tracker.get_zones(): cv2.polylines(overlay, [np.int32(z.get_coords())], 1, (0, 255, 255), 2) frame = cv2.addWeighted(overlay, 0.3, frame, 0.7, 0) # # count+=1 # video_writer.write(frame) # if patches: # for i, patch in enumerate(patches): # cv2.imshow("patch" + str(i), patch) # cv2.waitKey(1) # cv2.resize(frame, ()) tracking_in_pipe.push(frame) # frame = cv2.resize(frame, (int(frame.shape[1] / 2), int(frame.shape[0] / 2))) # cv2.imshow("output", frame) # cv2.waitKey(1)
def add_bg(background, foreground): merge = cv2.addWeighted(foreground, 0.3, background, 0.7, 0) plt.imshow(merge) plt.show()