def imcont(image,rangemax,hardthresh): r,th = cv2.threshold(image,rangemax,255,cv2.THRESH_TRUNC) #this now maxes out at max without stretching dynamic range (i.e. 26 in image is 26 in th if max > 26). adj = ((255/float(rangemax)) * th).astype(np.uint8) #now stretch dynamic range so that your max is 255. bf = cv2.boxFilter(adj, -1, (5,5)) #filter twice to get rid of noise that could be picked as a para. bf2 = cv2.boxFilter(bf, -1,(5,5)) r,th2 = cv2.threshold(bf2, hardthresh, 255, cv2.THRESH_BINARY) #now set a hard threshold. your choice here is made easier by your spread of the dynamic range. return th2.astype(np.uint8)
def raycast(image, rays=4, resolution=1, ret_type="image", dist_delta=1, blur=False): """ :param image: must be grayscale :param rays: number of rays to cast. must be 2, 4 or 8. 2 is left-right, 4 gives cardinal directions, 8 adds diagonal at 45 degrees. :param ret_type: "image" for all of the things composited, "array" for a list of separate frames :return: four or eight channel image of ray lengths, clockwise from up """ if not ret_type in ["image", "array"]: print "invalid return type in raycast call" return init_im = image lr_ray_im = left_right_ray(image, resolution, dist_delta) image = cv2.flip(image, 1) rl_ray_im = cv2.flip(left_right_ray(image, resolution), 1, dist_delta) if rays in [2, 4, 8]: if ret_type == "image": ret = cv2.merge([lr_ray_im, rl_ray_im]) elif ret_type == "array": ret = [lr_ray_im, rl_ray_im] if rays in [4, 8]: image = cv2.transpose(image) down_ray_im = cv2.transpose(left_right_ray(image, resolution, dist_delta), 1) image = cv2.flip(image, 1) up_ray_im = cv2.flip(cv2.transpose(cv2.flip(left_right_ray(image, resolution, dist_delta), 1)), 1) if ret_type == "image": ret = cv2.merge([up_ray_im, lr_ray_im, down_ray_im, rl_ray_im]) elif ret_type == "array": ret = [up_ray_im, lr_ray_im, down_ray_im, rl_ray_im] if rays == 8: skew_1, deskew_1 = skew(init_im) sca_ne_sw = [deskew(x, reverse_affine=deskew_1) for x in raycast(skew_1, rays=2, ret_type="array", resolution=resolution, dist_delta=1.414)] skew_2, deskew_2 = skew(cv2.flip(init_im, 0)) sca_se_nw = [cv2.flip(deskew(x, reverse_affine=deskew_2), 0) for x in raycast(skew_2, rays=2, ret_type="array", resolution=resolution, dist_delta=1.414)] array = [up_ray_im, sca_ne_sw[0], lr_ray_im, sca_se_nw[0], down_ray_im, sca_ne_sw[1], rl_ray_im, sca_se_nw[1]] if ret_type == "image": ret = cv2.merge(array) elif ret_type == "array": ret = array if blur: box_size = (resolution, resolution) if ret_type == "array": print "printing ret for investigative purposes:" print ret print "len(ret)", len(ret) print "ret[0]", type(ret[0]), ret[0] print "ret[1]", type(ret[1]), ret[1] ret = [cv2.boxFilter(x, -1, box_size, normalize=False) for x in ret] if ret_type == "image": ret = cv2.boxFilter(x, -1, box_size, normalize=False) return ret
def subsample_chroma(vertical=2, horizontal=2): """ Reduce the resolution of the chroma channels in the YCbCr image. The channels are filtered using a box filter (=average filter) """ crf = cv2.boxFilter(img_YCrCb[:,:,1], ddepth=-1, ksize=(horizontal, vertical)) cbf = cv2.boxFilter(img_YCrCb[:,:,2], ddepth=-1, ksize=(horizontal, vertical)) im_sub = img_YCrCb[:,:,:] # make a copy of the original im_sub[:,:,1] = crf im_sub[:,:,2] = cbf return im_sub
def explore_match(win, img1, img2, kp_pairs, status = None, H = None): h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] vis = img2 if H is not None: corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]]) corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2)) # cv2.polylines(vis, [corners], True, (255, 255, 255)) # cv2.fillPoly(vis, [corners], (255, 255, 255)) mask = np.zeros(vis.shape, dtype=np.uint8) roi_corners = np.array([[(corners[0][0],corners[0][1]), (corners[1][0],corners[1][1]), (corners[2][0],corners[2][1]), (corners[3][0], corners[3][1])]], dtype=np.int32) white = (255, 255, 255) cv2.fillPoly(mask, roi_corners, white) # apply the mask masked_image = cv2.bitwise_and(vis, mask) # blurred_image = cv2.blur(vis, (15, 15), 0) blurred_image = cv2.boxFilter(vis, -1, (27, 27)) vis = vis + (cv2.bitwise_and((blurred_image-vis), mask))
def calcGST(inputIMG, w): ## [calcGST_proto] img = inputIMG.astype(np.float32) # GST components calculation (start) # J = (J11 J12; J12 J22) - GST imgDiffX = cv.Sobel(img, cv.CV_32F, 1, 0, 3) imgDiffY = cv.Sobel(img, cv.CV_32F, 0, 1, 3) imgDiffXY = cv.multiply(imgDiffX, imgDiffY) ## [calcJ_header] imgDiffXX = cv.multiply(imgDiffX, imgDiffX) imgDiffYY = cv.multiply(imgDiffY, imgDiffY) J11 = cv.boxFilter(imgDiffXX, cv.CV_32F, (w,w)) J22 = cv.boxFilter(imgDiffYY, cv.CV_32F, (w,w)) J12 = cv.boxFilter(imgDiffXY, cv.CV_32F, (w,w)) # GST components calculations (stop) # eigenvalue calculation (start) # lambda1 = J11 + J22 + sqrt((J11-J22)^2 + 4*J12^2) # lambda2 = J11 + J22 - sqrt((J11-J22)^2 + 4*J12^2) tmp1 = J11 + J22 tmp2 = J11 - J22 tmp2 = cv.multiply(tmp2, tmp2) tmp3 = cv.multiply(J12, J12) tmp4 = np.sqrt(tmp2 + 4.0 * tmp3) lambda1 = tmp1 + tmp4 # biggest eigenvalue lambda2 = tmp1 - tmp4 # smallest eigenvalue # eigenvalue calculation (stop) # Coherency calculation (start) # Coherency = (lambda1 - lambda2)/(lambda1 + lambda2)) - measure of anisotropism # Coherency is anisotropy degree (consistency of local orientation) imgCoherencyOut = cv.divide(lambda1 - lambda2, lambda1 + lambda2) # Coherency calculation (stop) # orientation angle calculation (start) # tan(2*Alpha) = 2*J12/(J22 - J11) # Alpha = 0.5 atan2(2*J12/(J22 - J11)) imgOrientationOut = cv.phase(J22 - J11, 2.0 * J12, angleInDegrees = True) imgOrientationOut = 0.5 * imgOrientationOut # orientation angle calculation (stop) return imgCoherencyOut, imgOrientationOut
def OnOffFM(i_gpyr): """On,off center-surround intensity maps""" onpyr = [] offpyr = [] for i in xrange(0,len(i_gpyr)): # scale 2,3,4 curi = i_gpyr[i] surround3 = cv2.boxFilter(curi, -1, (7,7)) surround7 = cv2.boxFilter(curi, -1, (15,15)) on3 = NoNegative(curi - surround3) on7 = NoNegative(curi - surround7) off3 = NoNegative(surround3 - curi) off7 = NoNegative(surround7 - curi) onpyr.append(on3+on7) offpyr.append(off3+off7) onmap = AcrossScaleAddition(onpyr) offmap = AcrossScaleAddition(offpyr) return onmap, offmap
def dense_edges(im, size=(30, 30), threshold=50): edges_im = edges(im) sum_in_box = cv.boxFilter(edges_im, -1, size, normalize=True) dense_edges = cv.adaptiveThreshold(sum_in_box, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 101, -10) return dense_edges
def filters(): image = cv2.imread(config.ORIGIN_IMAGE) blur = [cv2.GaussianBlur(image, (5, 5), 0)] blur.append(cv2.medianBlur(image, 5)) blur.append(cv2.boxFilter(image, 5, (5, 5))) for i, x in enumerate(blur): cv2.imwrite( config.RESULT_PATH + "%s_mode_%s.jpg" % ( config.current_filename(__file__), i), x)
def get_H1(i): log("Fourier transforming on slice %d..." % i, 3) ff = fftn(images[i]) first_harmonic = ff[1, :, :] log("Inverse Fourier transforming on slice %d..." % i, 3) result = np.absolute(ifftn(first_harmonic)) log("Performing Gaussian blur on slice %d..." % i, 3) #result = cv2.GaussianBlur(result, (5, 5), 0) result = cv2.boxFilter(result, -1, (5, 5)) #result = cv2.bilateralFilter(result, 9, 75, 75) return result
def _find_new_box(self, heatmap): heatmap = cv2.boxFilter(heatmap, ddepth=-1, ksize=(self.recovery_kernel_size, self.recovery_kernel_size), borderType=cv2.BORDER_REPLICATE) # new_bb = np.array(np.unravel_index(np.argmax(conv_prob), conv_prob.shape))[::-1] # new_bb = np.array(tuple(new_bb - max(1, min(10, self.recovery_kernel_size/2))) + (min(20, self.recovery_kernel_size), ) * 2) # _, bb_reset = cv2.CamShift(heatmap, tuple(new_bb), self.term_crit) peak = np.unravel_index(np.argmax(heatmap), heatmap.shape) _, heatmap = cv2.threshold(heatmap, np.max(heatmap)*0.9, 255, cv2.THRESH_BINARY) _, bb_reset = cv2.floodFill(heatmap, None, tuple(peak[::-1]), 255, loDiff=10, flags=cv2.FLOODFILL_FIXED_RANGE) bb_area = bb_reset[2] * bb_reset[3] mean_prob_reset = np.sum(heatmap[bb_reset[1]:bb_reset[1] + bb_reset[3], bb_reset[0]:bb_reset[0] + bb_reset[2]]) / float(bb_area) return bb_reset, bb_area, mean_prob_reset
def LocalFigureNorm(src,figureRatio=0.2): """ Subtract src from boxFilter version of itself, with radius 0.2 of its side. Parameters: ========== - figureRatio is approximated figure's size compared to src's width. """ h,w = src.shape[:2] th,tw = int(h*figureRatio), int(w*figureRatio) figApprx = cv2.boxFilter(src, -1, (tw,tw)) return WNorm(MinMaxNorm(NoNegative(src-figApprx)))
def explore_match(win, img1, img2, kp_pairs, status = None, H = None): h1, w1 = img1.shape[:2] h2, w2 = img2.shape[:2] vis = img2 if H is not None: corners = np.float32([[0, 0], [w1, 0], [w1, h1], [0, h1]]) corners = np.int32( cv2.perspectiveTransform(corners.reshape(1, -1, 2), H).reshape(-1, 2)) # cv2.polylines(vis, [corners], True, (255, 255, 255)) # cv2.fillPoly(vis, [corners], (255, 255, 255)) mask = np.zeros(vis.shape, dtype=np.uint8) roi_corners = np.array([[(corners[0][0],corners[0][1]), (corners[1][0],corners[1][1]), (corners[2][0],corners[2][1]), (corners[3][0], corners[3][1])]], dtype=np.int32) white = (255, 255, 255) cv2.fillPoly(mask, roi_corners, white) # apply the mask masked_image = cv2.bitwise_and(vis, mask) # blurred_image = cv2.blur(vis, (15, 15), 0) blurred_image = cv2.boxFilter(vis, -1, (27, 27)) vis = vis + (cv2.bitwise_and((blurred_image-vis), mask)) # if status is None: # status = np.ones(len(kp_pairs), np.bool_) # p2 = np.int32([kpp[1].pt for kpp in kp_pairs]) # green = (0, 255, 0) # red = (0, 0, 255) # white = (255, 255, 255) # kp_color = (51, 103, 236) # for (x, y), inlier in zip(p2, status): # if inlier: # col = green # cv2.circle(vis, (x, y), 2, col, -1) # view params width, height = 1280, 800 x_offset = 260 y_offset = 500 l_img = create_blank(width, height, rgb_color=(0,0,0)) vis = np.append(vis, vis, axis=1) vis = cv2.resize(vis, (0,0), fx=0.6, fy=0.6) l_img[y_offset:y_offset+vis.shape[0], x_offset:x_offset+vis.shape[1]] = vis cv2.namedWindow(win, cv2.WND_PROP_FULLSCREEN) cv2.setWindowProperty(win, cv2.WND_PROP_AUTOSIZE, cv2.cv.CV_WINDOW_AUTOSIZE) cv2.imshow(win, l_img)
def niBlackThreshold( src, blockSize, k, binarizationMethod= 0 ): mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE) sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE) variance = sqmean - (mean*mean) stddev = np.sqrt(variance) thresh = mean + stddev * float(-k) thresh = thresh.astype(src.dtype) k = (src>thresh)*255 k = k.astype(np.uint8) return k # cv2.imshow()
def show_im(): #(h,s,v) = cv2.split(im_hsv) #h_mask = min_max_thresh(h,h_min,h_max) #s_mask = min_max_thresh(s,s_min,s_max) #v_mask = min_max_thresh(v,v_min,v_max) #tot_mask = cv2.bitwise_and(cv2.bitwise_and(h_mask,s_mask),v_mask) #masked_im = cv2.bitwise_and(im,cv2.merge([tot_mask,tot_mask,tot_mask])) #cv2.imshow("THRESH",masked_im) #dx = cv2.Sobel(h,8,1,0) #dy = cv2.Soble(h,8,0,1) #cv2.imshow("LAP",dx^2+dy^2) imgray = cv2.cvtColor( im,cv2.COLOR_BGR2GRAY) meanfiltered = imgray - cv2.boxFilter(imgray,8,(filtsize,filtsize))+128 meanfiltered = cv2.blur(meanfiltered,(3,1)) #cv2.imshow("THRESH",meanfiltered) gauss1 = cv2.GaussianBlur(imgray,(dog_k,dog_k),dog_sig1/10.) gauss2 = cv2.GaussianBlur(imgray,(dog_k,dog_k),dog_sig2/10.) DoG = gauss1-gauss2 #cv2.imshow("DoG",DoG) canny_edges = cv2.Canny(meanfiltered,canny_thresh1,canny_thresh2) k = cv2.getStructuringElement(cv2.MORPH_DILATE,(5,5)) canny_edges = cv2.dilate(canny_edges,k) #k = cv2.getStructuringElement(cv2.MORPH_ERODE,(3,3)) #canny_edges = cv2.erode(canny_edges,k) cv2.imshow("CANNY",cv2.resize(canny_edges,(canny_edges.shape[1]/2,canny_edges.shape[0]/2))) lines = np.array([]) #HOUGH_IM=canny_edges r,imbw = cv2.threshold(DoG,bwthresh,255,cv2.THRESH_BINARY) #cv2.imshow("THRESH",imbw) r,imbw_simple = cv2.threshold(imgray,bwthresh,255,cv2.THRESH_BINARY_INV) #cv2.imshow("THRESH",imbw_simple) HOUGH_IM=canny_edges #lines = cv2.HoughLinesP(HOUGH_IM,rho/10.,theta_disc*np.pi/180.,hough_thresh,minLineLength=minlinesize,maxLineGap=maxgap) lines = cv2.HoughLines(HOUGH_IM,rho/10.,theta_disc*np.pi/180.,hough_thresh,lines) imcp = im.copy() vert_im,horiz_im = draw_hough_lines(imcp,lines) corners = vert_im & horiz_im & HOUGH_IM #if lines != None and len(lines) > 0: # for ln in lines[0]: # pt1 = (ln[0],ln[1]) # pt2 = (ln[2],ln[3]) # disp = (ln[2]-ln[0],ln[3]-ln[1]) # length = (disp[0]**2+disp[1]**2) # theta = np.arctan(disp[1]/float(disp[0])) # #if abs(theta) < np.pi/8: # if abs(theta) > np.pi/2-np.pi/8: # cv2.line(imcp,pt1,pt2,(0,0,255)) imcp = canny_edges & horiz_im cv2.imshow("HOUGH",cv2.resize(imcp,(imcp.shape[1]/2,imcp.shape[0]/2)))
def sum_closed_edges(im, size = (31, 31)): """Combine dense_edges and contour_enclosed to give a high response in areas that edges are dense and contours are enclosed. """ edges_im = edges(im) sum_in_box = cv.boxFilter(edges_im, -1, size, normalize=True) enclosed = cv.GaussianBlur(contour_enclosed(im), size, 5) weighted = cv.addWeighted(sum_in_box, 0.5, enclosed, 0.5, 0) thresh = cv.adaptiveThreshold(sum_in_box, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 101, -10) return thresh
def doIt(args): img = cv2.imread(args[0]) if(img == None): print "Error: cannot load image " + args[0] return 1 imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #convert to grayscale #Simple GaussianBlur, medianBlur and boxFilter examples # imgFilt = cv2.GaussianBlur(imgGray,(3,3),1) #sigmaX = 1, sigmaY = 1 #imgFilt = cv2.medianBlur(imgGray,3) #imgFilt = cv2.boxFilter(imgGray,-1,(3,3)) #Uncomment the following for boxFilter example: cols = imgGray.shape[1] rows = imgGray.shape[0] imgFilt = np.zeros((rows,cols),imgGray.dtype) #create output image cv2.boxFilter(imgGray,-1,(3,3),imgFilt,(-1,-1),True,cv2.BORDER_REPLICATE) #all optional parameters used #Uncomment the following for filter2D example: #kernel = (0.1111111)*np.ones((3,3),np.float32) #cols = imgGray.shape[1] #rows = imgGray.shape[0] #imgFilt = np.zeros((rows,cols),imgGray.dtype) #create output image #cv2.filter2D(imgGray,-1,kernel,imgFilt) if len(args) > 1: cv2.imwrite(args[1], imgFilt) else: cv2.namedWindow(args[0]) cv2.imshow(args[0], imgGray) cv2.namedWindow("filtered") cv2.imshow("filtered", imgFilt) cv2.waitKey(0) return 0
def blurring(self, **kwargs): #image processing title = kwargs.get('title', 'Blurring') img = kwargs.get('img', None) fig = plt.figure(figsize=(12, 12)) fig.subplots_adjust(hspace=.1, wspace=.03) fig.suptitle( title, fontsize=25, y=.96) #core self._plot(fig, 1, img, 'original') bimg = cv2.boxFilter(img, 0, (15, 15)) self._plot(fig, 2, bimg, 'Averaging(Mean) Filtering') mimg = cv2.medianBlur(img, 15) self._plot(fig, 3, mimg, 'Median Filtering') gimg = cv2.GaussianBlur(img, (15, 15), 5) self._plot(fig, 4, gimg, 'Guassian Filtering') self._hide_ticks(fig) return fig
def Guidedfilter(im,p,r,eps): mean_I = cv2.boxFilter(im,cv2.CV_64F,(r,r)) mean_p = cv2.boxFilter(p, cv2.CV_64F,(r,r)) mean_Ip = cv2.boxFilter(im*p,cv2.CV_64F,(r,r)) cov_Ip = mean_Ip - mean_I*mean_p mean_II = cv2.boxFilter(im*im,cv2.CV_64F,(r,r)) var_I = mean_II - mean_I*mean_I a = cov_Ip/(var_I + eps) b = mean_p - a*mean_I mean_a = cv2.boxFilter(a,cv2.CV_64F,(r,r)) mean_b = cv2.boxFilter(b,cv2.CV_64F,(r,r)) q = mean_a*im + mean_b return q
def smooth(I, n=30): (w, h) = I.shape S = np.copy(I) R = np.zeros((w, h), dtype=np.int32) Hs = np.zeros((n, 1), dtype=np.float32) Us = np.zeros((n, 1), dtype=np.float32) Hr = np.zeros((n, 1), dtype=np.float32) Ur = np.zeros((n, 1), dtype=np.float32) for i in range(0, n): print "calculating for i = ", i S = cv2.boxFilter(S, ddepth=0, ksize=(3, 3)) R = I - S Cs = cglm(S) Cr = cglm(R) Hs[i], Us[i] = measure(Cs) Hr[i], Ur[i] = measure(Cr) return S, R, (Hs, Us), (Hr, Ur)
def main(): image = cv2.imread("../data/7.1.01.tiff", 1) ''' # Kernal or Convolution matrix for Identity Filter kernal = np.array(([0, 0, 0], [0, 1, 0], [0, 0, 0]), np.float32) ''' ''' # Kernal or Convolution matrix for Edge Detection kernal = np.array(([-1, -1, -1], [-1, 8, -1], [-1, -1, -1]), np.float32) ''' # Kernal or Convolution matrix for Box BLue Filter kernal = np.ones((5, 5), np.uint8) / 25 output = cv2.filter2D(image, -1, kernal) # Low pass filters implementation box_blur = cv2.boxFilter(image, -1, (31, 31)) simple_blur = cv2.blur(image, (21, 21)) gaussian_blur = cv2.GaussianBlur(image, (51, 51), 0) cv2.imshow("Orignal Image", image) cv2.imshow("Filtered Image", output) cv2.imshow("Box Blur", box_blur) cv2.imshow("Simple Blur", simple_blur) cv2.imshow("Gaussian Blur", gaussian_blur) cv2.waitKey(0) cv2.destroyAllWindows()
def process(self, heatmap, previous_bb=None, peak=None): """ This method computes a bounding box around the heatmap peak. Arguments: heatmap - target position heat map (may have multiple local maxima). peak - [y, x] of the most likely target position (usually the highest peak of the heatmap). This argument tells compute_bounding_box() which local maximum to choose. returns a bounding box array (x_upper_left,y_upper_left,width,height) or None if it can't be found """ if np.max(heatmap) == 0.0: return BoundingRegion() heatmap = cv2.boxFilter(heatmap, ddepth=-1, ksize=(self.recovery_kernel_size, self.recovery_kernel_size), borderType=cv2.BORDER_REPLICATE) if peak is None: peak = np.unravel_index(np.argmax(heatmap), heatmap.shape) if np.issubdtype(heatmap.dtype, np.float): _, heatmap = cv2.threshold(heatmap, self.heatmap_threshold*(1.0/255), 255, cv2.THRESH_BINARY) else: _, heatmap = cv2.threshold(heatmap, self.heatmap_threshold, 255, cv2.THRESH_BINARY) if heatmap[peak[0], peak[1]] != 255: cors = np.nonzero(heatmap) new_peak = None if len(cors[0]) > 0: dist2 = (cors[0] - peak[0])**2 + (cors[1] - peak[1])**2 ind=np.argmin(dist2) if dist2[ind] < self.distance_threshold**2: new_peak = np.array([cors[0][ind], cors[1][ind]]) if new_peak is None: return BoundingRegion(image_shape=(heatmap.shape[0], heatmap.shape[1], 3), box=np.array([peak[1], peak[0], 1, 1])) peak = new_peak _, bounding_box = cv2.floodFill(heatmap, None, tuple(peak[::-1]), 255, loDiff=10, flags=cv2.FLOODFILL_FIXED_RANGE) return BoundingRegion(image_shape=(heatmap.shape[0], heatmap.shape[1], 3), box=np.asarray(bounding_box))
def correlation_pnr(Y, gSig=None, center_psf=True, swap_dim=True, background_filter='disk'): """ compute the correlation image and the peak-to-noise ratio (PNR) image. If gSig is provided, then spatially filtered the video. Args: Y: np.ndarray (3D or 4D). Input movie data in 3D or 4D format gSig: scalar or vector. gaussian width. If gSig == None, no spatial filtering center_psf: Boolearn True indicates subtracting the mean of the filtering kernel swap_dim: Boolean True indicates that time is listed in the last axis of Y (matlab format) and moves it in the front Returns: cn: np.ndarray (2D or 3D). local correlation image of the spatially filtered (or not) data pnr: np.ndarray (2D or 3D). peak-to-noise ratios of all pixels/voxels """ if swap_dim: Y = np.transpose( Y, tuple(np.hstack((Y.ndim - 1, list(range(Y.ndim))[:-1])))) # parameters _, d1, d2 = Y.shape data_raw = Y.reshape(-1, d1, d2).astype('float32') # filter data data_filtered = data_raw.copy() if gSig: if not isinstance(gSig, list): gSig = [gSig, gSig] ksize = tuple([int(2 * i) * 2 + 1 for i in gSig]) if center_psf: if background_filter == 'box': for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur( img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) \ - cv2.boxFilter(img, ddepth=-1, ksize=ksize, borderType=1) else: psf = cv2.getGaussianKernel(ksize[0], gSig[0], cv2.CV_32F).dot( cv2.getGaussianKernel(ksize[1], gSig[1], cv2.CV_32F).T) ind_nonzero = psf >= psf[0].max() psf -= psf[ind_nonzero].mean() psf[~ind_nonzero] = 0 for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.filter2D(img, -1, psf, borderType=1) # data_filtered[idx, ] = cv2.filter2D(img, -1, psf, borderType=1) else: for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur( img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) # compute peak-to-noise ratio data_filtered -= data_filtered.mean(axis=0) data_max = np.max(data_filtered, axis=0) data_std = get_noise_fft(data_filtered.T, noise_method='mean')[0].T pnr = np.divide(data_max, data_std) pnr[pnr < 0] = 0 # remove small values tmp_data = data_filtered.copy() / data_std tmp_data[tmp_data < 3] = 0 # compute correlation image cn = local_correlations_fft(tmp_data, swap_dim=False) return cn, pnr
def niBlackThreshold( src, maxValue = 255, binarizationtype = cv2.THRESH_BINARY, blockSize = 23, k = 1.0, binarizationMethod = BINARIZATION_METHOD.BINARIZATION_WOLF): #Input grayscale image assert len(src.shape) == 2 assert blockSize % 2 == 1 and blockSize > 1 if (binarizationMethod == BINARIZATION_METHOD.BINARIZATION_SAUVOLA): assert src.dtype == np.uint8 # Compute local threshold (T = mean + k * stddev) # using mean and standard deviation in the neighborhood of each pixel # (intermediate calculations are done with floating-point precision) # note that: Var[X] = E[X^2] - E[X]^2 mean = cv2.boxFilter(src, cv2.CV_32F, (blockSize,blockSize),src, (-1,-1), True, cv2.BORDER_REPLICATE) sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize,blockSize), src, (-1,-1), True, cv2.BORDER_REPLICATE); variance = sqmean - mean * mean; variance[variance<0] = 0 stddev = np.sqrt(variance); if binarizationMethod == BINARIZATION_METHOD.BINARIZATION_NIBLACK: thresh = mean + stddev * k; elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_SAUVOLA: thresh = mean * (1. + k * (stddev / 128.0 - 1.)); elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_WOLF: srcMin = src.min() stddevMax = stddev.max() thresh = mean - k * (mean - srcMin - stddev * (mean - srcMin) / stddevMax); elif binarizationMethod == BINARIZATION_METHOD.BINARIZATION_NICK: sqrtVarianceMeanSum = np.sqrt(variance + sqmean) thresh = mean + k * sqrtVarianceMeanSum; else: assert "Unknown binarization method" thresh = thresh.astype(src.dtype) # Apply thresholding: ( pixel > threshold ) ? foreground : background if binarizationtype == cv2.THRESH_BINARY or binarizationtype == cv2.THRESH_BINARY_INV: # dst = (src > thresh) ? maxval : 0 # dst = (src > thresh) ? 0 : maxval cmpop = cv2.CMP_GT if binarizationtype == cv2.THRESH_BINARY else cv2.CMP_LE mask = cv2.compare(src, thresh, cmpop); mask = (mask==255) dst = np.zeros_like(src) dst[mask] = maxValue elif binarizationtype == cv2.THRESH_TRUNC: # dst = (src > thresh) ? thresh : src mask = cv2.compare(src, thresh, cv2.CMP_GT); mask = (mask==255) dst = src.copy() dst[mask] = thresh[mask] elif binarizationtype == cv2.THRESH_TOZERO or binarizationtype == cv2.THRESH_TOZERO_INV: # dst = (src > thresh) ? src : 0 cmpop = cv2.CMP_GT if binarizationtype == cv2.THRESH_TOZERO else cv2.CMP_LE mask = cv2.compare(src, thresh, cmpop) mask = (mask==255) dst = np.zeros_like(src) dst[mask] = src[mask] else: assert "Unknown threshold binarizationtype" return dst
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("../../../datas/imgs/Faces.jpg") # ## 归一化卷积框 - blur # blur = cv2.blur( img, ( 5, 5 ) ) # plt.subplot( 121 ), plt.imshow( img ), plt.title( 'Original' ) # plt.xticks( [] ), plt.yticks( [] ) # plt.subplot( 122 ), plt.imshow( blur ), plt.title( 'Blurred' ) # plt.xticks( [] ), plt.yticks( [] ) # plt.show() ## boxfilter blur = cv2.boxFilter(img, 2, (11, 11), normalize=False) plt.subplot(121), plt.imshow(img), plt.title("Original") plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(blur), plt.title("Blurred") plt.xticks([]), plt.yticks([]) plt.show() while True: cv2.imshow("image", blur) k = cv2.waitKey(10) & 0xFF c = chr(k) if c in ["q", "Q"]: break cv2.destroyAllWindows()
1 1 1 cv2.blur(img, (3, 3)) 进行均值滤波 参数说明:img表示输入的图片, (3, 3) 表示进行均值滤波的方框大小 cv2.boxfilter(img, -1, (3, 3), normalize=True) 表示进行方框滤波, 参数说明当normalize=True时,与均值滤波结果相同, normalize=False,表示对加和后的结果不进行平均操作,大于255的使用255表示 ''' #注意:如果你不想使用归一化卷积框,你应该使用 cv2.boxFilter(),这时要传入参数 normalize=False。 import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread('../images/Ball.png') # cv2读取出来的是bgr,图像处理时先转为rgb img = img[:, :, [2, 1, 0]] blur = cv2.blur(img, (3, 3)) boxfilter = cv2.boxFilter(img, -1, (3, 3), normalize=True) plt.subplot(131), plt.imshow(img), plt.title('Original') plt.xticks([]), plt.yticks([]) plt.subplot(132), plt.imshow(blur), plt.title('Blurred') plt.xticks([]), plt.yticks([]) plt.subplot(133), plt.imshow(boxfilter), plt.title('boxfilter') plt.xticks([]), plt.yticks([]) plt.show()
import cv2 #opencv读取的格式是BGR import numpy as np import CommonUtil import matplotlib.pyplot as plt imgPath = '../data/tliangtrans.jpg' img = cv2.imread(imgPath) # 均值滤波 # 简单的平均卷积操作 blur = cv2.blur(img, (3, 3)) # 方框滤波 # 基本和均值一样,可以选择归一化 box1 = cv2.boxFilter(img, -1, (3, 3), normalize=True) # 方框滤波 # 基本和均值一样,可以选择归一化,容易越界 box2 = cv2.boxFilter(img, -1, (3, 3), normalize=False) # 高斯滤波 # 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的 aussian = cv2.GaussianBlur(img, (5, 5), 1) # 中值滤波 # 相当于用中值代替 median = cv2.medianBlur(img, 5) # 中值滤波 titles = [ 'Original Image', 'Mean', 'Box normalize', 'Box FalseNorm', 'Gaussian',
def calc_tpi(dem, size): """ OpenCV implemntation of TPI, updated to ignore NaN values. Parameters ---------- dem : np.ndarray Expects a MaskedArray with a fill value of the DEM nodata value size : Size of kernel along one axis. Square kernel used. Returns ---------- np.ndarray (masked) : TPI """ logger.info('Computing TPI with kernel size {}...'.format(size)) # Set up 'kernel' window = (size, size) window_count = size * size # Get original mask # mask = copy.deepcopy(dem.mask) # # Change fill value to -9999 to ensure NaN corrections are applied correctly # nodata_val = -9999 # dem.fill_value = nodata_val nodata_val = dem.fill_value # Count the number of non-NaN values in the window for each pixel logger.debug('Counting number of valid pixels per window...') num_valid_window = cv2.boxFilter(np.logical_not(dem.mask).astype(int), -1, window, normalize=False, borderType=cv2.BORDER_REPLICATE) # Change masked values to fill value dem = dem.data # Get raw sum within window for each pixel logger.debug('Getting raw sum including NoData values...') sum_window = cv2.boxFilter(dem, -1, window, normalize=False, borderType=cv2.BORDER_REPLICATE) # Correct these sums by removing (fill value*number of times it was include in the sum) for each pixel logger.debug('Correcting for inclusion of NoData values...') sum_window = np.where(num_valid_window != window_count, # if not all pixels in window were valid # remove NoData val from sum for each time it was included sum_window + (-nodata_val * (window_count - num_valid_window)), sum_window) # Compute TPI (value - mean of value in window) logger.debug('Calculating TPI...') with np.errstate(divide='ignore', invalid='ignore'): tpi = dem - (sum_window / num_valid_window) # Remask any originally masked pixels tpi = np.ma.masked_where(dem.data == nodata_val, tpi) # tpi = np.ma.masked_where(mask, tpi) return tpi # def calc_tpi(dem, size): """ OpenCV implementation of TPI dem: array size: int, kernel size in x and y directions (square kernel) Note - borderType determines handline of edge cases. REPLICATE will take the outermost row and columns and extend them as far as is needed for the given kernel size. """ logger.info('Computing TPI with kernel size {}...'.format(size)) # To attempt to clean up artifacts that I believe are a results of # no data values being included in interpolation. So interpolate # closest values to no data instead of no data. values that were no # data are masked out again at the end. # Interpolate nodata, cubic method leaves no data at edges # logger.debug('Interpolating no data values...') # dem = interpolate_nodata(dem, method='cubic') # Clean up edges with simple nearest interpolation logger.debug('Cleaning up edge no data values...') dem = interpolate_nodata(dem, method='nearest') kernel = np.ones((size,size),np.float32)/(size*size) # -1 indicates new output array dem_conv = cv2.filter2D(dem, -1, kernel, borderType=cv2.BORDER_REPLICATE) tpi = dem - dem_conv return tpi
def _MainWindow__transform_to_box_filter(image): return cv2.boxFilter(image, 0, (7,7))
def blurring_average(img): return cv2.boxFilter(img , -1, (3,3))
def rgb2acf(img, pool_size=4): # smooth smooth_kernel = np.array([[1., 2., 1.]]) / 4. img_smooth = cv2.filter2D(img, cv2.CV_32F, smooth_kernel) # calc gradient gradient_kernel = np.array([[-1., 0., 1.]]) img_dx = cv2.filter2D(img_smooth, cv2.CV_32F, gradient_kernel) img_dy = cv2.filter2D(img_smooth, cv2.CV_32F, gradient_kernel.T) img_mag = cv2.magnitude(img_dx, img_dy) max_channel = np.argmax(img_mag, axis=-1) pos = (np.arange(img.shape[0])[:, None], np.arange(img.shape[1]), max_channel) img_dx = img_dx[pos] img_dy = img_dy[pos] img_grad = img_mag[pos] # normalized gradient magnitude exp_grad = cv2.boxFilter(img_grad, cv2.CV_32F, (11, 11)) # to be replaced by triangle filter norm_grad = img_grad / (exp_grad + 0.005) # gradient angle img_dx = np.where((img_dx >= 0) & (img_dx < 0.1), 0.1, img_dx) img_dx = np.where((img_dx >= -0.1) & (img_dx < 0), -0.1, img_dx) grad_angle = np.arctan(img_dy / img_dx) grad_angle[(img_dx < 0) & (img_dy > 0)] += np.pi grad_angle[(img_dx < 0) & (img_dy < 0)] -= np.pi # hog channels of 6 bins hog_channels = np.zeros((img.shape[0], img.shape[1], 6), dtype=np.float32) for i in range(6): angle_start = i * np.pi / 3. - np.pi angle_end = angle_start + np.pi / 3. hog_channels[:, :, i] = np.where( ((grad_angle > angle_start) & (grad_angle <= angle_end)), norm_grad, 0) # rgb to luv img_smooth /= 255. img_luv = cv2.cvtColor(img_smooth, cv2.COLOR_RGB2LUV) # normalize luv img_luv[:, :, 1] += 88. img_luv[:, :, 2] += 134. img_luv /= 270. # concatenate channels img_channels = np.concatenate( (norm_grad[:, :, None], hog_channels, img_luv), axis=-1) # 4*4 sum pooling pooling_shape = (img_channels.shape[0] // pool_size, img_channels.shape[1] // pool_size, img_channels.shape[2]) img_pooling = np.zeros(pooling_shape, dtype=np.float32) for i in range(pooling_shape[0]): for j in range(pooling_shape[1]): img_pooling[i, j, :] = np.sum( img_channels[i * pool_size:(i + 1) * pool_size, j * pool_size:(j + 1) * pool_size, :], axis=(0, 1)) # smooth smooth_kernel = np.array([[1., 2., 1.]]) / 4. features = cv2.filter2D(img_pooling, cv2.CV_32F, smooth_kernel) return features
image = imgt.copy() P1 = image[1:-1, 1:-1] valid = np.where(P1 == 1) P1, P2, P3, P4, P5, P6, P7, P8, P9 = P1[valid], image[2:, 1:-1][valid], image[ 2:, 2:][valid], image[1:-1, 2:][valid], image[:-2, 2:][ valid], image[:-2, 1:-1][valid], image[:-2, :-2][valid], image[ 1:-1, :-2][valid], image[2:, :-2][valid] CN = init.transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) ending_index = np.where(CN == 1) bifur_index = np.where(CN == 3) ending = np.asarray((valid[0][ending_index] + 1, valid[1][ending_index] + 1)) bifur = np.asarray((valid[0][bifur_index] + 1, valid[1][bifur_index] + 1)) imgfored = cv2.boxFilter(imgfore, -1, (9, 9)) imgfored[np.where(imgfored > 0)] = 255 edge1, edge2 = np.where(imgfored[ending[0], ending[1]] == 255), np.where( imgfored[bifur[0], bifur[1]] == 255) ending = np.delete(ending.T, edge1[0], 0) bifur = np.delete(bifur.T, edge2[0], 0) ending, theta1 = init.validateMinutiae(image, ending, 1) bifur, theta2 = init.validateMinutiae(image, bifur, 0) ending, bifur = ending.T, bifur.T plt.imshow(image, cmap='gray') plt.plot(ending[1], ending[0], 'b.', bifur[1], bifur[0], 'r.') plt.quiver(ending[1], ending[0], np.cos(theta1), np.sin(-theta1),
cv2.imshow(filename, img) cv2.imwrite(''.join([filename, ".png"]), img) src = cv2.imread("../Test_images/Lenna.png", 1) showandwrite("Original image", src) noise_img = src.copy() mean = 0 sigma = 50 win = (7, 7) add_gaussian_Noise(noise_img, mean, sigma) showandwrite("Gaussian Noise", noise_img) noise_dst = noise_img.copy() cv2.boxFilter(noise_dst, -1, win) showandwrite("GN Box filter", noise_dst) noise_dst1 = noise_img.copy() cv2.GaussianBlur(noise_dst1, win, 1.5) showandwrite("GN Gaussian filter", noise_dst1) noise_dst2 = noise_img.copy() cv2.medianBlur(noise_dst2, 5) showandwrite("GN Median filter", noise_dst1) noise_img2 = src.copy() pa = 0.01 pb = 0.01 add_sault_peper_Noise(noise_img2, pa, pb) showandwrite("Salt and Pepper Noise", noise_img2)
def correlation_pnr(Y, gSig=None, center_psf=True, swap_dim=True): """ compute the correlation image and the peak-to-noise ratio (PNR) image. If gSig is provided, then spatially filtered the video. Args: Y: np.ndarray (3D or 4D). Input movie data in 3D or 4D format gSig: scalar or vector. gaussian width. If gSig == None, no spatial filtering center_psf: Boolearn True indicates subtracting the mean of the filtering kernel swap_dim: Boolean True indicates that time is listed in the last axis of Y (matlab format) and moves it in the front Returns: cn: np.ndarray (2D or 3D). local correlation image of the spatially filtered (or not) data pnr: np.ndarray (2D or 3D). peak-to-noise ratios of all pixels/voxels """ if swap_dim: Y = np.transpose( Y, tuple(np.hstack((Y.ndim - 1, list(range(Y.ndim))[:-1])))) # parameters _, d1, d2 = Y.shape data_raw = Y.reshape(-1, d1, d2).astype('float32') # filter data data_filtered = data_raw.copy() if gSig: if not isinstance(gSig, list): gSig = [gSig, gSig] ksize = tuple([int(3 * i / 2) * 2 + 1 for i in gSig]) # create a spatial filter for removing background # psf = gen_filter_kernel(width=ksize, sigma=gSig, center=center_psf) if center_psf: for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur(img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) \ - cv2.boxFilter(img, ddepth=-1, ksize=ksize, borderType=1) # data_filtered[idx, ] = cv2.filter2D(img, -1, psf, borderType=1) else: for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur(img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) # compute peak-to-noise ratio data_filtered -= np.mean(data_filtered, axis=0) data_max = np.max(data_filtered, axis=0) data_std = get_noise_fft(data_filtered.transpose())[0].transpose() # data_std = get_noise(data_filtered, method='diff2_med') pnr = np.divide(data_max, data_std) pnr[pnr < 0] = 0 # remove small values tmp_data = data_filtered.copy() / data_std tmp_data[tmp_data < 3] = 0 # compute correlation image cn = local_correlations_fft(tmp_data, swap_dim=False) return cn, pnr
import cv2 o = cv2.imread(r"..\lena.jpg") r = cv2.boxFilter(o, -1, (2, 2), normalize=0) cv2.imshow("original", o) cv2.imshow("result", r) cv2.waitKey() cv2.destroyAllWindows()
import numpy as np import cv2 WIDTH = 128 # has a great influence on the result if __name__ == '__main__': img = cv2.imread('test.jpg', 0) img = cv2.resize(img, (WIDTH,WIDTH*img.shape[0]/img.shape[1])) c = cv2.dft(np.float32(img), flags = cv2.DFT_COMPLEX_OUTPUT) mag = np.sqrt(c[:,:,0]**2 + c[:,:,1]**2) spectralResidual = np.exp(np.log(mag) - cv2.boxFilter(np.log(mag), -1, (3,3))) c[:,:,0] = c[:,:,0] * spectralResidual / mag c[:,:,1] = c[:,:,1] * spectralResidual / mag c = cv2.dft(c, flags = (cv2.DFT_INVERSE | cv2.DFT_SCALE)) mag = c[:,:,0]**2 + c[:,:,1]**2 cv2.normalize(cv2.GaussianBlur(mag,(9,9),3,3), mag, 0., 1., cv2.NORM_MINMAX) cv2.imshow('Saliency Map', mag) c = cv2.waitKey(0) & 0xFF if(c==27 or c==ord('q')): cv2.destroyAllWindows()
# -*- coding: utf-8 -*- import cv2 o = cv2.imread(r"../image/lenaNoise.png") r = cv2.boxFilter(o, -1, (5, 5), normalize=0) cv2.imshow("original", o) cv2.imshow("result", r) cv2.waitKey() cv2.destroyAllWindows()
import cv2 import numpy as np from Tkinter import Tk from tkFileDialog import askopenfilename Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file print(filename) image = cv2.imread(filename) print ('If you want "smooth" press: 1') toDo = int (input('If you want "blur" press: 2')) cv2.imshow("original image",image) if toDo == 2: blur = cv2.boxFilter(image,-1,(5,5)) cv2.imshow("blur_image",blur) elif toDo == 1: smooth = cv2.GaussianBlur(image,(5,5), 0) cv2.imshow("smooth_image",smooth) cv2.waitKey(0) cv2.destroyAllWindows()
help='the thresh value for vert distance diff', default=10) args = argparser.parse_args() print(args) tic = time.time() file_name = '/home/kartik/Boeing/images/Nov 13 - Weird Edges - Multiple Pics/IMG_0071.JPG' file_name = '/home/kartik/Boeing/images/Nov 13 - Weird Edges - Multiple Pics/IMG_' + args.image_num + '.JPG' ##find the coarse position of the edges using the iterative canny update hough lines lines_params = RefinedHough.find_edges(file_name, 10, line_thickness=20) img = cv2.imread(file_name) img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) img_smooth = cv2.boxFilter(img_grey, ddepth=-1, ksize=(5, 5)) img_draw = cv2.imread(file_name) if args.edge_num == -1: local_edge_x_ind, local_edge_y_ind = find_local_gradient_points( img_smooth, lines_params[0], 90, 5200, 5, perp_num_points=51, visualise=1, img_draw=img_draw) else: local_edge_x_ind, local_edge_y_ind = find_local_gradient_points( img_smooth,
def correlation_pnr(Y, gSig=None, center_psf:bool=True, swap_dim:bool=True, background_filter:str='disk') -> Tuple[np.ndarray, np.ndarray]: """ compute the correlation image and the peak-to-noise ratio (PNR) image. If gSig is provided, then spatially filtered the video. Args: Y: np.ndarray (3D or 4D). Input movie data in 3D or 4D format gSig: scalar or vector. gaussian width. If gSig == None, no spatial filtering center_psf: Boolean True indicates subtracting the mean of the filtering kernel swap_dim: Boolean True indicates that time is listed in the last axis of Y (matlab format) and moves it in the front background_filter: str (undocumented) Returns: cn: np.ndarray (2D or 3D). local correlation image of the spatially filtered (or not) data pnr: np.ndarray (2D or 3D). peak-to-noise ratios of all pixels/voxels """ if swap_dim: Y = np.transpose( Y, tuple(np.hstack((Y.ndim - 1, list(range(Y.ndim))[:-1])))) # parameters _, d1, d2 = Y.shape data_raw = Y.reshape(-1, d1, d2).astype('float32') # filter data data_filtered = data_raw.copy() if gSig: if not isinstance(gSig, list): gSig = [gSig, gSig] ksize = tuple([int(2 * i) * 2 + 1 for i in gSig]) if center_psf: if background_filter == 'box': for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur( img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) \ - cv2.boxFilter(img, ddepth=-1, ksize=ksize, borderType=1) else: psf = cv2.getGaussianKernel(ksize[0], gSig[0], cv2.CV_32F).dot( cv2.getGaussianKernel(ksize[1], gSig[1], cv2.CV_32F).T) ind_nonzero = psf >= psf[0].max() psf -= psf[ind_nonzero].mean() psf[~ind_nonzero] = 0 for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.filter2D(img, -1, psf, borderType=1) # data_filtered[idx, ] = cv2.filter2D(img, -1, psf, borderType=1) else: for idx, img in enumerate(data_filtered): data_filtered[idx, ] = cv2.GaussianBlur( img, ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=1) # compute peak-to-noise ratio data_filtered -= data_filtered.mean(axis=0) data_max = np.max(data_filtered, axis=0) data_std = get_noise_fft(data_filtered.T, noise_method='mean')[0].T pnr = np.divide(data_max, data_std) pnr[pnr < 0] = 0 # remove small values tmp_data = data_filtered.copy() / data_std tmp_data[tmp_data < 3] = 0 # compute correlation image cn = local_correlations_fft(tmp_data, swap_dim=False) return cn, pnr
4、每次紫色矩形向右移动时,实际上就是求对应的蓝色矩形的像素和, 此时只要把上一次的求和结果减去蓝色矩形内的第一个红色块,再加上它右面的一个红色块, 就是当前位置的和了,用公式表示 sum[i] = sum[i-1] - buff[x-1] + buff[x+m-1] 5、当紫色矩形移动到行尾时,需要对buff进行更新。因为整个绿色矩形下移了一个像素, 所以对于每个buff[i], 需要加上一个新进来的像素,再减去一个出去的像素,然后便开始新的一行的计算了。 Boxfilter的初始化过程非常快速,每个矩形的计算基本上只需要一加一减两次运算。 从初始化的计算速度上来说,Boxfilter比Integral Image要快一些,大约25%。 在具体求某个矩形特征时,Boxfilter比Integral Image快4倍, 所谓的4倍其实就是从4次加减运算降低到1次,虽然这个优化非常渺小, 但把它放到几层大循环里面,还是能节省一些时间的。对于那些实时跟踪检测算法, 一帧的处理时间要严格在40ms以下,正是这些细小的优化决定了程序的效率,积少成多,聚沙成塔。 ''' result1 = cv2.boxFilter(img, -1, (5, 5)) # 方框滤波:boxFilter ''' 二:GaussianBlur高斯滤波与低通滤波器不同,低通滤波器中每个像素的权重是相同的,即滤波器是线性的。 而高斯滤波器中像素的权重与其距中心像素的距离成比例。 在一些需要对一个像素的周围像素给予更多的重视,可通过分配权重来重新计算这些周围点值。 可通过高斯函数(钟形函数,即喇叭形数)的权重方案来解决。 ''' result = cv2.GaussianBlur(img, (5, 5), 1.5) ''' 三:中值滤波器是非线性滤波器,对消除椒盐现象特别有用, result = cv2.medianBlur(image,5) image -- 原图像 5 -- 孔径尺寸,一个大于1的奇数。5 中值滤波器会使用 5 x 5 的范围来计算。 即对像素的中心值及其 5 x 5 邻域组成了一个数值集,对其进行处理计算,当前像素被其中值替换掉。 如某个像素周围有白或黑的像素,这些白或黑色的像素不会被选择作为中值(最大或最小值不用),而是被替换为邻域值,示例如下:
#imshow(imgN) #cv2.imwrite('test.jpg',img) #start=time.clock() # img = cv2.imread('pic3.tif', 0) #img1=100*(img-np.mean(img)) #img1[np.where(img1>255)]=255 #blockSize=51 #kernel=np.ones((blockSize,blockSize))/(blockSize**2) #img2=cv2.filter2D(img1,-1,kernel) #img3=img2.copy() #img3[np.where(img2>150)]=255; img3[np.where(img2<=150)]=0 #plt.subplot(1,3,1) #imshow(img) #plt.subplot(1,3,2) #imshow(img1) #plt.subplot(1,3,3) #imshow(img2) #plt.figure() #imshow(img3) img = 100 * (img - np.mean(img)) blockSize = 31 img[np.where(img > 255)] = 255 img = cv2.boxFilter(img, -1, (blockSize, blockSize)) img[np.where(img > 150)] = 255 img[np.where(img <= 150)] = 0 img = cv2.boxFilter(img, -1, (31, 31)) img[np.where(img > 0)] = 255 print np.min(img) imshow(img)
def align_matrices(coor_list, bcr_header, bcr_array, rots_count, rots_count_around_z,refine, ref_angle, docker_rough_output, ref_line_num, \ up_down_steps_count, cb, scale,rmsd ,gauss, boxcar_size): # cb is corner background avg_background1 = np.sum( bcr_array[bcr_array.shape[0] - cb:bcr_array.shape[0], bcr_array.shape[1] - cb:bcr_array.shape[1]]) / cb**2 avg_background2 = np.sum( bcr_array[bcr_array.shape[0] - cb:bcr_array.shape[0], :cb]) / cb**2 avg_background3 = np.sum( bcr_array[:cb, bcr_array.shape[1] - cb:bcr_array.shape[1]]) / cb**2 avg_background4 = np.sum(bcr_array[:cb, :cb]) / cb**2 avg_backgrounds = [ avg_background1, avg_background2, avg_background3, avg_background4 ] avg_background = (np.sum(bcr_array[bcr_array.shape[0]-cb:bcr_array.shape[0],bcr_array.shape[1]-cb:bcr_array.shape[1]]) + \ np.sum(bcr_array[bcr_array.shape[0]-cb:bcr_array.shape[0],:cb]) + np.sum(bcr_array[:cb,bcr_array.shape[1]-cb:bcr_array.shape[1]]) + \ np.sum(bcr_array[:cb,:cb]))/(4*cb*cb) #print(avg_backgrounds) #print(avg_background) if (all(abs(1 - (i / avg_background)) <= 0.35 for i in avg_backgrounds) == False): print( "The afm image is in the corner or you set too small -o parameter where background size vary in big range. Try to crop image with bigger background or set bigger -o parameter" ) sys.exit() #avg_bck_count = 4*cb*cb #sys.exit() pdb_matrices, list_of_all_rots, list_of_axisangles, list_of_all_angles_z = pdb_rots_to_bins( coor_list, bcr_header, rots_count, rots_count_around_z, refine, ref_angle, docker_rough_output, ref_line_num) bcr_array = np.array(bcr_array) #print(bcr_array) mat2_afm_max = bcr_array.max() mat2_afm_min = bcr_array.min() mat2_afm_range = mat2_afm_max - avg_background def get_max_min_range(mat1_pdb): mat1_pdb_max = mat1_pdb.max() mat1_pdb_min = mat1_pdb.min() mat1_pdb_range = mat1_pdb_max - mat1_pdb_min return (mat1_pdb_max, mat1_pdb_min, mat1_pdb_range) def scale_matrices(mat1_pdb): p1max, p1min, p1range = get_max_min_range(mat1_pdb) mat1_pdb = avg_background + ( (mat2_afm_range) * ((mat1_pdb - p1min) / (p1range))) # resp. miesto min tam moze ist average background min_val, top_left = opencv_align(bcr_array, mat1_pdb) return (top_left, mat1_pdb) # p1min will be the def move_up_down(mat1_pdb): p1max, p1min, p1range = get_max_min_range(mat1_pdb) bol_pdb = mat1_pdb > 0.01 # I guess, that 1/10 Angstrom is biggest value I care about mat1_pdb = mat1_pdb + avg_background # pdb must be with zero backgroud step = (mat2_afm_max - p1max) / up_down_steps_count l = 0 #bol_pdb = abs(mat1_pdb - avg_background) > 0.0001 # all background pixels set to 0 (False), non background px are 1 (true) #print(bol_pdb) score_new = sys.maxsize - 1 # maximal available integer score_old = sys.maxsize mat1_pdb_mov = mat1_pdb while l <= up_down_steps_count and score_new < score_old: score_old = score_new mat1_pdb_mov_old = mat1_pdb_mov mat1_pdb_mov = mat1_pdb + bol_pdb * l * step # lever up only non background pixels (bol pdb is bolean matrix- 1for pixels > average) score_new, top_left = opencv_align( bcr_array, mat1_pdb_mov) # after levering up, we need to align l = l + 1 return (top_left, mat1_pdb_mov_old ) # old pdb will have lower score than new one aligned_matrices = [] korel_sums = [] matrices_of_diffs = [] gauss_1 = int(gauss[1]) print("Aligning pdb matrices to bcr.") for k in range(0, len(pdb_matrices)): #iterate trough list of matrices pdb_array = np.array(pdb_matrices[k]) pdb_array_shape = pdb_array.shape #maska2 = pdb_array_with_surroundings > 0.000 #maska2 = maska2*100 #pdb_array_new = pdb_array_with_surroundings[np.ix_(maska2.any(1),maska2.any(0))] #draw_points_test(pdb_array_new,"{}_pdb_array_new.png".format(str(k))) if (not (gauss == [None, None])): #pdb_array = ndimage.gaussian_filter(pdb_array_with_surroundings, gauss_sigma, order=0) pdb_array_with_surroundings = np.full( (pdb_array_shape[0] + ((int(gauss_1) - 1) * 2), pdb_array_shape[1] + ((int(gauss_1) - 1) * 2)), 0.000) pdb_array_with_surroundings[int(gauss_1-1): pdb_array_shape[0]+gauss_1-1, \ int(gauss_1-1): pdb_array_shape[1]+gauss_1-1] = pdb_array pdb_array = cv2.GaussianBlur(pdb_array_with_surroundings, ksize=(gauss_1, gauss_1), sigmaX=gauss[0], sigmaY=0, borderType=cv2.BORDER_CONSTANT) #cropp #draw_points_test(pdb_array,"{}_gauss".format(str(k))) #mask = pdb_array > 0.0001 # for zeroes true, for non zeroes false #pdb_array = pdb_array[np.ix_(mask.any(1),mask.any(0))] elif (boxcar_size is not None): pdb_array_with_surroundings = np.full( (pdb_array_shape[0] + ((int(boxcar_size) - 1) * 2), pdb_array_shape[1] + ((int(boxcar_size) - 1) * 2)), 0.000) pdb_array_with_surroundings[int(boxcar_size-1): pdb_array_shape[0]+boxcar_size-1, \ int(boxcar_size-1): pdb_array_shape[1]+boxcar_size-1] = pdb_array #pdb_array = ndimage.uniform_filter(pdb_array_with_surroundings, boxcar_size) pdb_array = cv2.boxFilter(pdb_array_with_surroundings, ksize=(boxcar_size, boxcar_size), borderType=cv2.BORDER_CONSTANT, ddepth=-1) #draw_points_test(pdb_array,"{}_poboxcar".format(str(k))) #cropp #mat_for_crop = (1000000000*pdb_array).astype(np.uint8) #mask = pdb_array > 0.0001 # for zeroes true, for non zeroes false #draw_points_test(mask,"{}_maska_furttonejde.png".format(str(k))) #mask = mask*100 #pdb_array = pdb_array[np.ix_(mask.any(1),mask.any(0))] pdb_array_shape = pdb_array.shape max_val_pdb = np.amax(pdb_array) kor_sum = 0 if scale == True: try: yx_dist, pdb_array = scale_matrices(pdb_array) y_dist, x_dist = yx_dist except cv2.error as e: print("Cv2 error") korel_sums.append(sys.float_info.max) matrices_of_diffs.append( np.full(bcr_array.shape, sys.float_info.max / 2)) aligned_matrices.append( np.full(bcr_array.shape, sys.float_info.max / 2)) continue else: try: yx_dist, pdb_array = move_up_down(pdb_array) y_dist, x_dist = yx_dist except cv2.error as e: print("Cv2 error") korel_sums.append(sys.float_info.max) matrices_of_diffs.append( np.full(bcr_array.shape, sys.float_info.max / 2)) aligned_matrices.append( np.full(bcr_array.shape, sys.float_info.max / 2)) continue new_pdb_array = np.full( (bcr_array.shape[0], bcr_array.shape[1]), avg_background) #new pdb array with shape of bcr array diff_matrix = np.copy(new_pdb_array) try: new_pdb_array[x_dist:x_dist + pdb_array_shape[0], y_dist:y_dist + pdb_array_shape[1]] = pdb_array except (IndexError, ValueError): print( "Index error or value error- if there are many of these outputs, the input bcr matrix is probably too small" ) continue aligned_matrices.append(new_pdb_array) if (rmsd == False): diff_matrix = abs(bcr_array - new_pdb_array) kor_sum = diff_matrix.sum() / bcr_array.size else: diff_matrix = np.sqrt((bcr_array - new_pdb_array)**2) kor_sum = diff_matrix.sum() / np.sqrt(bcr_array.size) korel_sums.append(kor_sum) matrices_of_diffs.append(diff_matrix) #sys.exit() return (list_of_axisangles, korel_sums, matrices_of_diffs, aligned_matrices, list_of_all_angles_z)
beta = 1.0 - alpha b = x[0] * beta + y[0] * alpha g = x[1] * beta + y[1] * alpha r = x[2] * beta + y[2] * alpha return [b, g, r] def verifycontour(c, (px, py)): ret = cv2.pointPolygonTest(c, (px, py), False) if ret >= 0: return True return False def painteye(roi, fullpic, (ex, ey), colormod): h, w, depth = roi.shape enchanced = cv2.medianBlur(roi, 7) window = cv2.min(cv2.boxFilter(enchanced, -1, (5, 9)), cv2.boxFilter(enchanced, -1, (9, 5))) single = cv2.split(window)[1] px, py = (w/2, h/2) gamma = 2 # this falling back decreases accuracy but though it works while gamma > 0: dst = cv2.adaptiveThreshold(single, 255, cv2.cv.CV_ADAPTIVE_THRESH_GAUSSIAN_C, cv2.cv.CV_THRESH_BINARY_INV, getblocksize(w, h), gamma) kern = np.ones(getkern(w, h), dtype=np.uint8) dst = cv2.dilate(dst, kern) contours, hierarchy = cv2.findContours(dst, cv2.cv.CV_RETR_EXTERNAL, cv2.cv.CV_CHAIN_APPROX_NONE) maxc = None for c in contours: if not verifycontour(c, (px, py)): continue if maxc is None or len(c) > len(maxc): maxc = c
import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread( "../../../datas/imgs/Faces.jpg" ) # ## 归一化卷积框 - blur # blur = cv2.blur( img, ( 5, 5 ) ) # plt.subplot( 121 ), plt.imshow( img ), plt.title( 'Original' ) # plt.xticks( [] ), plt.yticks( [] ) # plt.subplot( 122 ), plt.imshow( blur ), plt.title( 'Blurred' ) # plt.xticks( [] ), plt.yticks( [] ) # plt.show() ## boxfilter blur = cv2.boxFilter(img, 2, ( 11, 11 ), normalize = False ) plt.subplot( 121 ), plt.imshow( img ), plt.title( 'Original' ) plt.xticks( [] ), plt.yticks( [] ) plt.subplot( 122 ), plt.imshow( blur ), plt.title( 'Blurred' ) plt.xticks( [] ), plt.yticks( [] ) plt.show() while( True ): cv2.imshow( "image", blur ) k = cv2.waitKey( 10 ) & 0xFF c = chr( k ) if c in ['q', 'Q']: break cv2.destroyAllWindows()
def noisyleaveout(good,gray,kp1,kp2,box,xx,yy): if len(good)>0: blank = np.zeros(gray.shape) # cv2.imshow('gray',gray) # cv2.waitKey(0) no_noisy_good = [] for i in good: (y1,x1) = kp1[i[0].queryIdx].pt # (y2,x2) = kp2[i[0].trainIdx].pt # print(blank.shape,gray.shape) # print(x1,y1) # cv2.waitKey(0) # print(box) if y1>= xx-2*box[2] and y1<= xx+2*box[2] and x1>= yy-2*box[3] and x1<= yy+2*box[3]: print('jugde:',x1,y1) blank[int(x1),int(y1)] = 255 # blank[int(x1),int(y1)] = 255 cv2.imshow('kp binary img',blank) # box filter # print(box) filtimg = cv2.boxFilter(blank,-1,(int(box[2]),int(box[3]))) # filtimg = cv2.boxFilter(filtimg,-1,(int(box[2]*2),int(box[3]*2))) cv2.imshow('filted img',filtimg) # find the highest density kp in the img # (the value is the biggest at that point after filting) min,max,min_loc,max_loc = cv2.minMaxLoc(filtimg) for i in good: (x1,y1) = kp1[i[0].queryIdx].pt # remove not good enough keypoint in drawing bound box # not drawkeypoint() if int(abs(x1-max_loc[0]))<= box[2] or int(abs(y1-max_loc[1]))<= box[3]: no_noisy_good.append(i) lst = [] if len(no_noisy_good)>0: for i in no_noisy_good: # tracking img1_idx = i[0].queryIdx img2_idx = i[0].trainIdx # print(good,'\n',good[0][0].queryIdx,good[0][0].trainIdx) # break (x1,y1) = kp1[img1_idx].pt (x2,y2) = kp2[img2_idx].pt # print((x1,y1),(x2,y2)) # rectangle position pt1 = (int(x1)-int(x2),int(y1)-int(y2)) pt2 = (int(pt1[0]+box[2]),int(pt1[1]+box[3])) lst.append((pt1,pt2)) # print('pt1:',pt1,'pt2:',pt2) # print(Counter(lst)) pt1,pt2 = Counter(lst).most_common(1)[0][0] # colors = tuple(list(colors)) # print(colors,type(colors),type((255,0,0)),type(colors[1])) # colors = (int(colors[0]),int(colors[1]),int(colors[2])) # cv2.rectangle(img,pt1,pt2,colors,2,1) return pt1,pt2 else: return 0,0 else: # tracking failure cv2.putText(img3, "detected failure",(100,20),cv2.FONT_ITALIC,0.75,(50,170,50),2) return 0,0
thiline = line.strip().split(' ') if thiline[0] != '#': colors.append([np.int(x) for x in thiline]) mask = 0 for color in colors: mask += cv2.inRange(im, np.array(color) - 5, np.array(color) + 5) #150,150,150 ''' cv2.imshow("mask", cv2.medianBlur(mask,5)) key = cv2.waitKey(5) while key == -1: key = cv2.waitKey(5) ''' mask = cv2.medianBlur(mask, 5) mask = cv2.boxFilter(mask, ddepth=-1, ksize=(65, 65)) mask[mask != 0] = 255 _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) #print(hierarchy) LENGTH = len(contours) if LENGTH != 0: rectList = [] for i, cnt in enumerate(contours): x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(mask, (x, y), (x + w, y + h), 255, cv2.FILLED) _, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
print "value "+str(v) if __name__ == "__main__": camera = cv2.VideoCapture(0) #create the window where the boxFiltered image will be shown cv2.namedWindow("boxFilter") #create a trackbar that controlls the ksize.X value cv2.createTrackbar("ksize.X","boxFilter",1,200,dummy) #create a trackbar that controlls the ksize.Y value cv2.createTrackbar("ksize.Y","boxFilter",1,200,dummy) #create a trackbar that controlls the ksize.Y value #cv2.createTrackbar("ddepth","boxFilter",2,10,dummy) while True: f,img = camera.read() #boxFilter the image with the selected parameters in the trackbars img2 = cv2.boxFilter(img,-1,(cv2.getTrackbarPos("ksize.X","boxFilter"),cv2.getTrackbarPos("ksize.Y","boxFilter"))) cv2.imshow("boxFilter",img2) if (cv2.waitKey(5) != -1): break
def get_face_landmarks_5(self, only_keep_largest=False, only_center_face=False, resize=None, blur_ratio=0.01, eye_dist_threshold=None): if resize is None: scale = 1 input_img = self.input_img else: h, w = self.input_img.shape[0:2] scale = min(h, w) / resize h, w = int(h / scale), int(w / scale) input_img = cv2.resize(self.input_img, (w, h), interpolation=cv2.INTER_LANCZOS4) with torch.no_grad(): bboxes = self.face_det.detect_faces(input_img, 0.97) * scale for bbox in bboxes: # remove faces with too small eye distance: side faces or too small faces eye_dist = np.linalg.norm([bbox[6] - bbox[8], bbox[7] - bbox[9]]) if eye_dist_threshold is not None and (eye_dist < eye_dist_threshold): continue if self.template_3points: landmark = np.array([[bbox[i], bbox[i + 1]] for i in range(5, 11, 2)]) else: landmark = np.array([[bbox[i], bbox[i + 1]] for i in range(5, 15, 2)]) self.all_landmarks_5.append(landmark) self.det_faces.append(bbox[0:5]) if len(self.det_faces) == 0: return 0 if only_keep_largest: h, w, _ = self.input_img.shape self.det_faces, largest_idx = get_largest_face(self.det_faces, h, w) self.all_landmarks_5 = [self.all_landmarks_5[largest_idx]] elif only_center_face: h, w, _ = self.input_img.shape self.det_faces, center_idx = get_center_face(self.det_faces, h, w) self.all_landmarks_5 = [self.all_landmarks_5[center_idx]] # pad blurry images if self.pad_blur: self.pad_input_imgs = [] for landmarks in self.all_landmarks_5: # get landmarks eye_left = landmarks[0, :] eye_right = landmarks[1, :] eye_avg = (eye_left + eye_right) * 0.5 mouth_avg = (landmarks[3, :] + landmarks[4, :]) * 0.5 eye_to_eye = eye_right - eye_left eye_to_mouth = mouth_avg - eye_avg # Get the oriented crop rectangle # x: half width of the oriented crop rectangle x = eye_to_eye - np.flipud(eye_to_mouth) * [-1, 1] # - np.flipud(eye_to_mouth) * [-1, 1]: rotate 90 clockwise # norm with the hypotenuse: get the direction x /= np.hypot(*x) # get the hypotenuse of a right triangle rect_scale = 1.5 x *= max(np.hypot(*eye_to_eye) * 2.0 * rect_scale, np.hypot(*eye_to_mouth) * 1.8 * rect_scale) # y: half height of the oriented crop rectangle y = np.flipud(x) * [-1, 1] # c: center c = eye_avg + eye_to_mouth * 0.1 # quad: (left_top, left_bottom, right_bottom, right_top) quad = np.stack([c - x - y, c - x + y, c + x + y, c + x - y]) # qsize: side length of the square qsize = np.hypot(*x) * 2 border = max(int(np.rint(qsize * 0.1)), 3) # get pad # pad: (width_left, height_top, width_right, height_bottom) pad = (int(np.floor(min(quad[:, 0]))), int(np.floor(min(quad[:, 1]))), int(np.ceil(max(quad[:, 0]))), int(np.ceil(max(quad[:, 1])))) pad = [ max(-pad[0] + border, 1), max(-pad[1] + border, 1), max(pad[2] - self.input_img.shape[0] + border, 1), max(pad[3] - self.input_img.shape[1] + border, 1) ] if max(pad) > 1: # pad image pad_img = np.pad(self.input_img, ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect') # modify landmark coords landmarks[:, 0] += pad[0] landmarks[:, 1] += pad[1] # blur pad images h, w, _ = pad_img.shape y, x, _ = np.ogrid[:h, :w, :1] mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w - 1 - x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h - 1 - y) / pad[3])) blur = int(qsize * blur_ratio) if blur % 2 == 0: blur += 1 blur_img = cv2.boxFilter(pad_img, 0, ksize=(blur, blur)) # blur_img = cv2.GaussianBlur(pad_img, (blur, blur), 0) pad_img = pad_img.astype('float32') pad_img += (blur_img - pad_img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0) pad_img += (np.median(pad_img, axis=(0, 1)) - pad_img) * np.clip(mask, 0.0, 1.0) pad_img = np.clip(pad_img, 0, 255) # float32, [0, 255] self.pad_input_imgs.append(pad_img) else: self.pad_input_imgs.append(np.copy(self.input_img)) return len(self.all_landmarks_5)
#encoding:utf-8 import cv2 import numpy as np import matplotlib.pyplot as plt #读取图片 img = cv2.imread('test01.jpg') source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #方框滤波 result = cv2.boxFilter(source, -1, (5, 5), normalize=0) #显示图形 titles = ['Source Image', 'BoxFilter Image'] images = [source, result] for i in xrange(2): plt.subplot(1, 2, i + 1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show()
sys.path.append("../../../") import cv2 import numpy as np if __name__ == '__main__': Img = cv2.imread('../../../Datas/OpencvLogo.png') cv2.imshow('Img', Img) # ---------------------------- Blur ---------------------------- # KernelSize = (5, 5) BlurImg = cv2.blur(src=Img, ksize=KernelSize) cv2.imshow('cvBlur', BlurImg) KernelSize = (5, 5) BoxFilterImg = cv2.boxFilter(src=Img, ddepth=-1, ksize=KernelSize, normalize=True) cv2.imshow('cvbox', BoxFilterImg) print np.equal(BlurImg, BoxFilterImg).all() cv2.waitKey() # ---------------------------- Gaussian Blur ---------------------------- # KernelSize = (15, 15) GaussianBlurImg = cv2.GaussianBlur(src=Img, ksize=KernelSize, sigmaX=0, sigmaY=0) # KernelSize = (5, 15) GaussianKernelX = cv2.getGaussianKernel(ksize=KernelSize[0], sigma=0) GaussianKernelY = cv2.getGaussianKernel(ksize=KernelSize[1], sigma=0) print 'GaussianKernelX:\n', GaussianKernelX print 'GaussianKernelY:\n', GaussianKernelY FilterGaussian = cv2.sepFilter2D(src=Img, ddepth=-1, kernelX=GaussianKernelX, kernelY=GaussianKernelY) cv2.imshow('Gauss', GaussianBlurImg)
def get_candidate_components(sv, dims, Yres_buf, min_num_trial=3, gSig=(5, 5), gHalf=(5, 5), sniper_mode=True, rval_thr=0.85, patch_size=50, loaded_model=None, test_both=False, thresh_CNN_noisy=0.5, use_peak_max=False, thresh_std_peak_resid = 1): """ Extract new candidate components from the residual buffer and test them using space correlation or the CNN classifier. The function runs the CNN classifier in batch mode which can bring speed improvements when multiple components are considered in each timestep. """ Ain = [] Ain_cnn = [] Cin = [] Cin_res = [] idx = [] ijsig_all = [] cnn_pos = [] local_maxima = [] Y_patch = [] ksize = tuple([int(3 * i / 2) * 2 + 1 for i in gSig]) half_crop_cnn = tuple([int(np.minimum(gs*2, patch_size/2)) for gs in gSig]) compute_corr = test_both if use_peak_max: img_select_peaks = sv.reshape(dims).copy() # plt.subplot(1,3,1) # plt.cla() # plt.imshow(img_select_peaks) img_select_peaks = cv2.GaussianBlur(img_select_peaks , ksize=ksize, sigmaX=gSig[0], sigmaY=gSig[1], borderType=cv2.BORDER_REPLICATE) \ - cv2.boxFilter(img_select_peaks, ddepth=-1, ksize=ksize, borderType=cv2.BORDER_REPLICATE) thresh_img_sel = np.median(img_select_peaks) + thresh_std_peak_resid * np.std(img_select_peaks) # plt.subplot(1,3,2) # plt.cla() # plt.imshow(img_select_peaks*(img_select_peaks>thresh_img_sel)) # plt.pause(.05) # threshold_abs = np.median(img_select_peaks) + np.std(img_select_peaks) # img_select_peaks -= np.min(img_select_peaks) # img_select_peaks /= np.max(img_select_peaks) # img_select_peaks *= 2**15 # img_select_peaks = img_select_peaks.astype(np.uint16) # clahe = cv2.createCLAHE(clipLimit=40.0, tileGridSize=(half_crop_cnn[0]//2,half_crop_cnn[0]//2)) # img_select_peaks = clahe.apply(img_select_peaks) local_maxima = peak_local_max(img_select_peaks, min_distance=np.max(np.array(gSig)).astype(np.int), num_peaks=min_num_trial,threshold_abs=thresh_img_sel, exclude_border = False) min_num_trial = np.minimum(len(local_maxima),min_num_trial) for i in range(min_num_trial): if use_peak_max: ij = local_maxima[i] else: ind = np.argmax(sv) ij = np.unravel_index(ind, dims, order='C') local_maxima.append(ij) ij = [min(max(ij_val,g_val),dim_val-g_val-1) for ij_val, g_val, dim_val in zip(ij,gHalf,dims)] ij_cnn = [min(max(ij_val,g_val),dim_val-g_val-1) for ij_val, g_val, dim_val in zip(ij,half_crop_cnn,dims)] ind = np.ravel_multi_index(ij, dims, order='C') ijSig = [[max(i - g, 0), min(i+g+1,d)] for i, g, d in zip(ij, gHalf, dims)] ijsig_all.append(ijSig) ijSig_cnn = [[max(i - g, 0), min(i+g+1,d)] for i, g, d in zip(ij_cnn, half_crop_cnn, dims)] indeces = np.ravel_multi_index(np.ix_(*[np.arange(ij[0], ij[1]) for ij in ijSig]), dims, order='F').ravel(order = 'C') indeces_ = np.ravel_multi_index(np.ix_(*[np.arange(ij[0], ij[1]) for ij in ijSig]), dims, order='C').ravel(order = 'C') Ypx = Yres_buf.T[indeces, :] ain = np.maximum(np.mean(Ypx, 1), 0) if sniper_mode: indeces_cnn = np.ravel_multi_index(np.ix_(*[np.arange(ij[0], ij[1]) for ij in ijSig_cnn]), dims, order='F').ravel(order = 'C') Ypx_cnn = Yres_buf.T[indeces_cnn, :] ain_cnn = Ypx_cnn.mean(1) else: compute_corr = True # determine when to compute corr coef na = ain.dot(ain) sv[indeces_] /= 1 # 0 if na: ain /= sqrt(na) Ain.append(ain) Y_patch.append(Ypx) idx.append(ind) if sniper_mode: Ain_cnn.append(ain_cnn) if sniper_mode & (len(Ain_cnn) > 0): Ain_cnn = np.stack(Ain_cnn) Ain2 = Ain_cnn.copy() Ain2 -= np.median(Ain2,axis=1)[:,None] Ain2 /= np.std(Ain2,axis=1)[:,None] Ain2 = np.reshape(Ain2,(-1,) + tuple(np.diff(ijSig_cnn).squeeze()),order= 'F') Ain2 = np.stack([cv2.resize(ain,(patch_size ,patch_size)) for ain in Ain2]) predictions = loaded_model.predict(Ain2[:,:,:,np.newaxis], batch_size=min_num_trial, verbose=0) keep_cnn = list(np.where(predictions[:, 0] > thresh_CNN_noisy)[0]) discard = list(np.where(predictions[:, 0] <= thresh_CNN_noisy)[0]) cnn_pos = Ain2[keep_cnn] else: keep_cnn = [] # list(range(len(Ain_cnn))) if compute_corr: keep_corr = [] for i, (ain, Ypx) in enumerate(zip(Ain, Y_patch)): ain, cin, cin_res = rank1nmf(Ypx, ain) Ain[i] = ain Cin.append(cin) Cin_res.append(cin_res) rval = corr(ain.copy(), np.mean(Ypx, -1)) if rval > rval_thr: keep_corr.append(i) keep_final = list(set().union(keep_cnn, keep_corr)) if len(keep_final) > 0: Ain = np.stack(Ain)[keep_final] else: Ain = [] Cin = [Cin[kp] for kp in keep_final] Cin_res = [Cin_res[kp] for kp in keep_final] idx = list(np.array(idx)[keep_final]) else: Ain = [Ain[kp] for kp in keep_cnn] Y_patch = [Y_patch[kp] for kp in keep_cnn] idx = list(np.array(idx)[keep_cnn]) for i, (ain, Ypx) in enumerate(zip(Ain, Y_patch)): ain, cin, cin_res = rank1nmf(Ypx, ain) Ain[i] = ain Cin.append(cin) Cin_res.append(cin_res) return Ain, Cin, Cin_res, idx, ijsig_all, cnn_pos, local_maxima
point = plt.ginput(1) block = np.floor(np.array(point) / B) print("Coordinates of selected block: ", block) scol = int(block[0, 0]) srow = int(block[0, 1]) plt.plot([B * scol, B * scol + B, B * scol + B, B * scol, B * scol], [B * srow, B * srow, B * srow + B, B * srow + B, B * srow]) plt.axis([0, w, h, 0]) # transform the picture from RGB to YCrCb transcol = cv2.cvtColor(img1, cv2.COLOR_BGR2YCR_CB) SSV = 2 SSH = 2 crf = cv2.boxFilter(transcol[:, :, 1], ddepth=-1, ksize=(2, 2)) cbf = cv2.boxFilter(transcol[:, :, 2], ddepth=-1, ksize=(2, 2)) crsub = crf[::SSV, ::SSH] cbsub = cbf[::SSV, ::SSH] imSub = [transcol[:, :, 0], crsub, cbsub] # the quantization table for Y and CbCr QY = np.array([[16, 11, 10, 16, 24, 40, 51, 61], [12, 12, 14, 19, 26, 48, 60, 55], [14, 13, 16, 24, 40, 57, 69, 56], [14, 17, 22, 29, 51, 87, 80, 62], [18, 22, 37, 56, 68, 109, 103, 77], [24, 35, 55, 64, 81, 104, 113, 92], [49, 64, 78, 87, 103, 121, 120, 101], [72, 92, 95, 98, 112, 100, 103, 99]])
def main(videoSource = 0, videoHeight=240, videoWidth=None, ROISize=5, gain=40, outputFilePath=None): """ This script reads in video frames of a person from a webcam or video and displays the spatial color change and computed PPG waveform over time. Parameters ---------- videoSource : int or string Provide an integer for the webcam ID you wish to use, or provide a path to a video file. The best results will be with a pre-recorded high-resolution and minimal-compression video. Default is to use the default webcam. Use Video.py to pre-record demo video. videoHeight : int Optional parameter for real-time webcam video height in pixels. Set to be as large as possible without stuttering the video. videoWidth : int Optional parameter for real-time webcam video width. Set to None to auto-config. ROISize : int The length on one side of the Region-of-interest square that we will average over gain : int The amplification factor of the output ROIs outputFilePath : string Optional path to an output video file generated from the spatial color change frames """ useFaceDetection = True # Plot timeseries, 200 frames wide plot = PlotRunningTimeseries(200, 3) if (type(videoSource) is int): videoCap = VideoCapture(videoSource,videoWidth = videoWidth, videoHeight = videoHeight) else: videoCap = VideoCapture(videoSource) # Find out the FOURCC code of the incoming video # (It should be as uncompressed as possible) print 'Incoming video FOURCC code: ' + \ str(struct.pack('>I', int(videoCap.cam.get(cv2.cv.CV_CAP_PROP_FOURCC)))) region = [] for i, frame in enumerate(videoCap): # Find a region to track if we don't have one already if region == []: regions = [] # Select regions to average over on soonest possible frame. # Only use 1 region for now as more necessitate a code refactor. if useFaceDetection: regions = FacesDetect(frame) else: regions = MouseSelectRegions(frame,count=1) if regions == []: continue # Pick the first (for FacesDetect.py, the largest) region to use for the tracker region = regions[0] # Initialize tracker using the region given above tracker = LKHomography(frame,region) # Container for the subsequent tracked regions # Extract coordinates from the detected region x1, y1, x2, y2 = region regionHeight = y2 - y1 regionWidth = x2 - x1 # Output needs to be the same size as downsampled region finalHeight = int(numpy.ceil(float(regionHeight - 1) / ROISize)) finalWidth = int(numpy.ceil(float(regionWidth - 1) / ROISize)) # Select a filter to use for tracking the baseline brightness value for each pixel # The simplest is a Moving Average (boxcar) filter, but others include a Kalman # Filter. Future improvements would be to make a filter sensitive to global lighting # changes. # Moving average filter (boxcar) with a history of 50 frames # The number of frames to average is the baseline = MovingAverage((finalHeight, finalWidth, 3), 30) writer = VideoWriter((regionHeight,regionWidth,3),outputFilePath=outputFilePath) # Track the region throughout the subsequent frames, warp the image so that # each pixel is aligned as much as possible during the sequence and crop each face out try: alignedRegion = tracker.TrackWarpCrop(frame) except Exception, e: region = [] continue cv2.imshow('Aligned Region', alignedRegion) # Normalize the amplitude of the underlying signal by multiplying # by the inverse of the average value of each pixel # #This is not implemented yet # #This makes the next downsample/averaging step more accurate in theory if (ROISize ** 2 * 256 > 2 ** 32): raise Exception('Too big of an ROISize for int32') ## COMPUTE AVERAGE OF EACH ROI ############################################ # Because we want to do find the average quickly, use an integer datatype and # only work with sums of values until we finally do the division at the # final amplification step. # Convolve with a box of ones to sum each ROI. Do *not* normalize the result # (dividing by size of the filter). Doing an entire convolution in C++ and # then selected the subset is way faster than doing this by hand in python. sumROI = cv2.boxFilter(numpy.int32(alignedRegion), -1, (ROISize, ROISize), normalize = False) # Extract the sum of each ROI using the value at the top left of each ROI # and ignoring the rest of the values as they're unneccessary sumROI = sumROI[0:-1:ROISize, 0:-1:ROISize, :] # Subtract the current frame from the time-average and then amplify and # normalize the result. You could further filter this output to only show # heart rate frequencies, but I feel it hinders debugging noise sources. # We need to show a reasonable color change in 8 bits. # Floating point values are returned here baselineValues = baseline.update(sumROI) # For the MIT video, the amplitude of the pulse is (40 / 49) ~ 1 bit per pixel # So the gain should be on the order of ... 50? Try and center it too by adding 128 amplifiedROI = ((sumROI - baselineValues) * gain) / (ROISize ** 2) + 128 # hist = numpy.histogram(amplifiedROI,bins=numpy.arange(-300,301,50)) # print hist # It's now in units of bits of resu amplifiedROI = numpy.uint8(amplifiedROI) result = cv2.resize(amplifiedROI, (regionWidth,regionHeight)) cv2.imshow('Result', result) # Plot the amplified mean value of the entire face over time plot.update(numpy.mean(numpy.mean(amplifiedROI, axis = 0), axis = 0)) # Write to an output video file if requested if outputFilePath != None: writer.Write(result) ch = 0xFF & cv2.waitKey(1) if ch == 27: # ESC Key break
import cv2 import numpy as np import matplotlib.pyplot as plt img = cv2.imread('E:/PythonProgram/opencv_study/fig_transaction/opencv.PNG') b, g, r = cv2.split(img) img = cv2.merge([r, g, b]) # 这两行代码解决rgb排列的问题 # 1、均值滤波 blur = cv2.blur(img, (5, 5)) # 2、高斯滤波 gblur = cv2.GaussianBlur(img, (5, 5), 0) # 3、中值滤波 median = cv2.medianBlur(img, 5) #4、盒子滤波 box = cv2.boxFilter(img, -1, (5, 5)) plt.subplot(231), plt.imshow(img), plt.title('Original') plt.xticks([]), plt.yticks([]) plt.subplot(232), plt.imshow(blur), plt.title('Blurred') plt.xticks([]), plt.yticks([]) plt.subplot(233), plt.imshow(gblur), plt.title('GaussianBlurred') plt.xticks([]), plt.yticks([]) plt.subplot(234), plt.imshow(median), plt.title('MedianBlurred') plt.xticks([]), plt.yticks([]) plt.subplot(235), plt.imshow(box), plt.title('BoxBlurred') plt.xticks([]), plt.yticks([]) plt.show()
def multi_render(self, obj_list, write=False): if write: fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('398_3.mp4',fourcc, 30.0, (960,540)) init_bgr = cv2.resize( cv2.imread(self.img_dir[0], flags=1), (0,0), fx=self.resize_rate, fy=self.resize_rate) bgr_h, bgr_w = init_bgr.shape[0:2] frame_num = len(self.img_dir) max_pt_num = 0 obj_num = len(obj_list) diff_thres = 0 hl_thres = 120 cur_mask = np.zeros_like(init_bgr) trace_mask = np.zeros_like(init_bgr) bgr_with_mask = np.zeros_like(init_bgr) for obj in obj_list: max_pt_num = max(max_pt_num, len(obj['new_pt_list'])) pre_img_idx = -1 for i in range(0, max_pt_num): cur_mask.fill(np.uint8(0)) for obj in obj_list: if i < len(obj['new_pt_list']): (cur_x,cur_y), cur_img_idx = obj['new_pt_list'][i] ref_x, ref_y = obj['pt_list'][cur_img_idx] delta_x, delta_y = cur_x-ref_x, cur_y-ref_y cur_obj_reg = obj['reg_list'][cur_img_idx] cur_obj_reg = [(cur_obj_reg[0][0]+delta_x, cur_obj_reg[0][1]+delta_y), (cur_obj_reg[1][0]+delta_x, cur_obj_reg[1][1]+delta_y)] if cur_obj_reg[1][0]>bgr_w or cur_obj_reg[1][1]>bgr_h: continue cur_obj_patch = obj['patch_list'][cur_img_idx] cur_mask[cur_obj_reg[0][1]:cur_obj_reg[1][1], cur_obj_reg[0][0]:cur_obj_reg[1][0]] = cur_obj_patch _, cur_mask_gray_hl = cv2.threshold(cv2.cvtColor(cur_mask, cv2.COLOR_BGR2GRAY), hl_thres, 0, cv2.THRESH_TOZERO) trace_mask_gray = cv2.cvtColor(trace_mask, cv2.COLOR_BGR2GRAY) diff = np.int32(cur_mask_gray_hl)-np.int32(trace_mask_gray) trace_mask = np.uint8(0.99*trace_mask) # trace_mask = np.where(np.expand_dims(cv2.cvtColor(trace_mask, cv2.COLOR_BGR2GRAY)>70, axis=3), trace_mask, np.uint8(0)) # trace_mask = np.where(np.expand_dims(np.logical_and(diff>diff_thres, trace_mask_gray==0), axis=3), np.uint8(1.2*cur_mask), np.uint8(trace_mask)) trace_mask = np.where(np.expand_dims(diff>diff_thres, axis=3), cur_mask, trace_mask) blur_trace_mask = trace_mask.copy() # blur_trace_mask = cv2.bilateralFilter(trace_mask, 5, 70, 70) # kernel_dilate = np.ones((3,3), np.uint8) # blur_trace_mask = cv2.dilate(blur_trace_mask, kernel_dilate, iterations = 1) blur_trace_mask = cv2.boxFilter(blur_trace_mask, -1, (3, 3), normalize=True) kernel_erode = np.ones((3,3), np.uint8) blur_trace_mask = cv2.erode(blur_trace_mask, kernel_erode, iterations = 1) if cur_img_idx != pre_img_idx: bgr = cv2.resize( cv2.imread(self.img_dir[cur_img_idx], flags=1), (0,0), fx=self.resize_rate, fy=self.resize_rate ) bgr_with_mask = np.where(np.expand_dims(cv2.cvtColor(blur_trace_mask, cv2.COLOR_BGR2GRAY)>cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY), axis=3), np.uint8(blur_trace_mask), bgr) cv2.imshow('mask', bgr_with_mask) if write: out.write(bgr_with_mask) k = cv2.waitKey(30) & 0xff if k == 27: break cv2.imshow('mask', bgr_with_mask) cv2.waitKey(-1) cv2.destroyAllWindows()
# -*- coding: utf-8 -*- # By:Eastmount CSDN 2021-06-07 import cv2 import numpy as np import matplotlib.pyplot as plt #读取图片 img = cv2.imread('lena.png') source = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #方框滤波 result = cv2.boxFilter(source, -1, (3, 3), normalize=1) #用来正常显示中文标签 plt.rcParams['font.sans-serif'] = ['SimHei'] #显示图形 titles = ['原始图像', '方框滤波'] images = [source, result] for i in range(2): plt.subplot(1, 2, i + 1), plt.imshow(images[i], 'gray') plt.title(titles[i]) plt.xticks([]), plt.yticks([]) plt.show()
def blur_img(img_LR, blur_algos=['clean'], kernel_size=0): h, w, c = img_LR.shape blur_type = random.choice(blur_algos) if blur_type == 'average': #Averaging Filter Blur (Homogeneous filter) if kernel_size == 0: kernel_size = int(np.random.uniform(3, 11)) if kernel_size > h: kernel_size = int(np.random.uniform(3, h / 2)) kernel_size = int(np.ceil(kernel_size)) if kernel_size % 2 == 0: kernel_size += 1 blurred = cv2.blur(img_LR, (kernel_size, kernel_size)) elif blur_type == 'box': #Box Filter Blur if kernel_size == 0: kernel_size = int(np.random.uniform(3, 11)) if kernel_size > h: kernel_size = int(np.random.uniform(3, h / 2)) kernel_size = int(np.ceil(kernel_size)) if kernel_size % 2 == 0: kernel_size += 1 blurred = cv2.boxFilter(img_LR, -1, (kernel_size, kernel_size)) elif blur_type == 'gaussian': #Gaussian Filter Blur if kernel_size == 0: kernel_size = int(np.random.uniform(3, 11)) if kernel_size > h: kernel_size = int(np.random.uniform(3, h / 2)) kernel_size = int(np.ceil(kernel_size)) if kernel_size % 2 == 0: kernel_size += 1 blurred = cv2.GaussianBlur(img_LR, (kernel_size, kernel_size), 0) elif blur_type == 'bilateral': #Bilateral Filter sigma_bil = int(np.random.uniform(20, 120)) if kernel_size == 0: kernel_size = int(np.random.uniform(3, 9)) if kernel_size > h: kernel_size = int(np.random.uniform(3, h / 2)) kernel_size = int(np.ceil(kernel_size)) if kernel_size % 2 == 0: kernel_size += 1 blurred = cv2.bilateralFilter(img_LR, kernel_size, sigma_bil, sigma_bil) elif blur_type == 'clean': # Pass clean image, without blur blurred = img_LR #img_LR = np.clip(blurred, 0, 1) return blurred, blur_type, kernel_size
def apply(self, img, blur_ksize=3, **params): img = cv2.boxFilter(img, ddepth=cv2.CV_8U, ksize=(blur_ksize, blur_ksize)) return img
plt.xlim([0, 256]) plt.legend(('cdf', 'histogram'), loc = 'upper right') plt.title('After Histogram Equalization') plt.show() yuv[:, :, 0] = img_y_histeq img_enhanced = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR) cv2.imwrite('input_y_enhanced.png', img_enhanced) # Example: filtering plt.figure(3) plt.imshow(img_y, cmap = 'gray') plt.title('Original') output = cv2.boxFilter(img_y, -1, (7, 7)) plt.figure(4) plt.imshow(output, cmap = 'gray') plt.title('Box Filter (7x7)') sigma = 3.0 radius = int(3*sigma) ksize = 2*radius + 1 output = cv2.GaussianBlur(img_y, (ksize, ksize), sigma) plt.figure(5) plt.imshow(output, cmap = 'gray') plt.title('Gaussian Filter (sigma = %.2f)' % sigma) output = cv2.medianBlur(img_y, 7) plt.figure(6) plt.imshow(output, cmap = 'gray')
def box_filter(I, r): return cv2.boxFilter(I, -1, (5, 5))