def optflowHornSchunk(new: np.ndarray, ref: np.ndarray, uv, smoothing=0.01) -> np.ndarray: if cv2 is not None: """ http://docs.opencv.org/modules/legacy/doc/motion_analysis.html Note that smoothness parameter for cv.CalcOpticalFlowHS needs to be SMALLER than matlab to get similar result. Useless when smoothness was 1 in OpenCV, but it's 1 in Matlab! """ cvref = cv2.fromarray(ref) cvnew = cv2.fromarray(new) # result is placed in u,v # matlab vision.OpticalFlow Horn-Shunck has default maxiter=10, terminate=eps, smoothness=1 cv2.CalcOpticalFlowHS( cvref, cvnew, False, uv[0], uv[1], smoothing, (cv2.CV_TERMCRIT_ITER | cv2.CV_TERMCRIT_EPS, 8, 0.1)) # reshape to numpy float32, xpix x ypix x 2 flow = dstack((asarray(uv[0]), asarray(uv[1]))) else: # use Python method u, v = HornSchunck(ref, new) flow = dstack((u, v)) return flow
def getDisparity(imgLeft, imgRight, method="BM"): gray_left = cv.cvtColor(imgLeft, cv.COLOR_BGR2GRAY) gray_right = cv.cvtColor(imgRight, cv.COLOR_BGR2GRAY) print(gray_left.shape) c, r = gray_left.shape if method == "BM": sbm = cv.StereoBM_create() disparity = cv.stereoRectify(c, r, cv.CV_32F) sbm.SADWindowSize = 11 sbm.preFilterType = 1 sbm.preFilterSize = 5 sbm.preFilterCap = 61 sbm.minDisparity = -50 sbm.numberOfDisparities = 112 sbm.textureThreshold = 507 sbm.uniquenessRatio = 0 sbm.speckleRange = 8 sbm.speckleWindowSize = 0 gray_left = cv.fromarray(gray_left) gray_right = cv.fromarray(gray_right) cv.FindStereoCorrespondenceBM(gray_left, gray_right, disparity, sbm) disparity_visual = cv.CreateMat(c, r, cv.CV_8U) cv.Normalize(disparity, disparity_visual, 0, 255, cv.CV_MINMAX) disparity_visual = np.array(disparity_visual) elif method == "SGBM": sbm = cv.StereoSGBM() sbm.SADWindowSize = 9 sbm.numberOfDisparities = 0 sbm.preFilterCap = 63 sbm.minDisparity = -21 sbm.uniquenessRatio = 7 sbm.speckleWindowSize = 0 sbm.speckleRange = 8 sbm.disp12MaxDiff = 1 sbm.fullDP = False disparity = sbm.compute(gray_left, gray_right) disparity_visual = cv.normalize(disparity, alpha=0, beta=255, norm_type=cv.cv.CV_MINMAX, dtype=cv.cv.CV_8U) return disparity_visual
def plotptrn(self, ptrnlst, xres=0, yres=0): ''' functions plots the modules and returns an image e.g. xres=5,yres=5 resuts in a 611x1221 image :param ptrnfile: result of the functions patternfiles ''' if xres == 0 or yres == 0: xres = self.xpermm yres = self.ypermm # i diode j pixel, np.int32 --> input to array xcor = np.fromfunction(lambda i, j: self.fxpos(i, j) * xres, (20 * self.nmod, ptrnlst[0].shape[1]), dtype=np.int32) ycor = np.fromfunction(lambda i, j: self.fypos(i, j) * yres, (20 * self.nmod, ptrnlst[0].shape[1]), dtype=np.int32) # here the output of the array is defined xcor = xcor.astype(np.int32, copy=False) ycor = ycor.astype(np.int32, copy=False) #ycor=ycor+abs(ycor.min()) # x and y cannot be negative if xcor.min() < 0: xcor += abs(xcor.min()) if ycor.min() < 0: ycor += abs(ycor.min()) # number of pixels ptrnfile.shape[1] a = np.zeros((ycor.max() + 1, xcor.max() + 1)) for i in range(0, 20 * self.nmod): #adding prevents zeroes from overwriting ones a[ycor[i, :], xcor[i, :]] += ptrnlst[i // 20][i % 20, :] img = cv2.fromarray(np.uint8(a * (255 / 3))) return img
def __getitem__(self, index): dataset = self.train_dataset if self.mode == True else self.test_dataset img_path, label_path = dataset[index] # Uniform image size #image = Image.open(img_path).convert("RGB") image = cv2.imread(img_path) #label = Image.open(label_path).convert("L") label = cv2.imread(label_path, 0) # Image resized to the same dimension image, label = Compose( [FreeScale(self.resize)])(image, label) if self.mode: if self.trainsform is not None: image, label = self.trainsform(image, label) # Convert it to pytorch style image = img_transform(image) mask = mask_transform(label) #edge = mask_transform(Image.fromarray(edge_contour(np.asarray(label)))) edge = mask_transform(cv2.fromarray(edge_contour(np.asarray(label)))) return image, mask, edge else: image = img_transform(image) mask = mask_transform(label) return image, mask
def show_seg_result(image, label, seg_label): """show (image, label, ground_truth)""" # prepare img npimage = np.array(torchvision.utils.make_grid(image)) # npimage /= 255 # normailze value nplabel = np.array((torchvision.utils.make_grid(label))) # nplabel /= 255 # normailze value npseg_label = np.array(torchvision.utils.make_grid(seg_label.detach())) npseg_label = npseg_label > 0.5 npseg_label = np.uint8(npseg_label * 255) # npseg_label /= 255 # normailze value # show image print('Image: {}'.format(npimage.shape)) # # use plt # plt.imshow(np.transpose(npimage, (1,2,0)), cmap='Accent') # plt.show() # use cv2 img = cv2.fromarray(npimage) cv2.namedWindow("Image") cv2.imshow("Image", img) cv2.waitKey(0) print('Ground truth: {}'.format(nplabel.shape)) # # use plt # plt.imshow(np.transpose(nplabel, (1,2,0)), cmap='Greys') # plt.show() # use cv2 label = cv2.fromarray(nplabel) cv2.namedWindow("Ground_truth") cv2.imshow("Ground_truth", label) cv2.waitKey(0) print('Seg result: {}'.format(npseg_label.shape)) # # use plt # plt.imshow(np.transpose(npseg_label, (1,2,0)), cmap='Accent') # plt.show() # use cv2 seg_label = cv2.fromarray(npseg_label) cv2.namedWindow("Seg result") cv2.imshow("Seg result", seg_label) cv2.waitKey(0) cv2.destroyAllWindows()
def logpolar(src, center, magnitude_scale=40): mat1 = cv.fromarray(numpy.float64(src)) mat2 = cv.CreateMat(src.shape[0], src.shape[1], mat1.type) cv.LogPolar(mat1, mat2, center, magnitude_scale, \ cv.CV_INTER_CUBIC+cv.CV_WARP_FILL_OUTLIERS) return numpy.asarray(mat2)
def detect(image): image_faces = [] bitmap = cv.fromarray(image) faces = cv.HaarDetectObjects(bitmap, cascade, cv.CreateMemStorage(0)) if faces: for (x, y, w, h), n in faces: image_faces.append(image[y:(y + h), x:(x + w)]) #cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,255),3) return image_faces
def cal_grad(B): B = B[0,...] H, W = B.shape #for i in range(H): # for j in range(W): B_cv2 = cv2.fromarray(B) B_lap = cv2.Laplacian(img,cv2.CV_64F) return np.array(B_lap)
def template_match(img, refimg, confidence=0.6, xwin=19, ywin=19): """ Return all matches of refimg inside img, using Template Matching. (Gratefully) borrowed from: http://stackoverflow.com/questions/7670112/finding-a-subimage-inside-a-numpy-image/9253805# 9253805 Input: obj img: A numpy array representing an image obj refimg: A numpy array representing the reference image float confidence: threshold value (from [0,1]) for template matching Output: A tuple of (x,y) coodinates, w.r.t the coordinate system of img. """ # OpenCV requires either uint8, or float, but with floats it got # buggy and failed badly (I think it had to do with it not # correctly handling when 'img' had no decimals, but 'refimg' # had decimal expansions, which I suppose means that internally # OpenCV.matchTemplate does exact integer comparisons. img = img.astype('uint8') refimg = refimg.astype('uint8') I = cv.fromarray(img) ref = cv.fromarray(refimg) # I = cv.fromarray(np.copy(img)) # ref = cv.fromarray(np.copy(refimg)) I_s = cv.CreateMat(I.rows, I.cols, I.type) cv.Smooth(I, I_s, cv.CV_GAUSSIAN, param1=xwin, param2=ywin) ref_s = cv.CreateMat(ref.rows, ref.cols, ref.type) cv.Smooth(ref, ref_s, cv.CV_GAUSSIAN, param1=xwin, param2=ywin) # img = np.array(img, dtype='uint8') # refimg = np.array(refimg, dtype='uint8') result = cv.CreateMat(I_s.rows - ref_s.rows + 1, I_s.cols - ref_s.cols + 1, cv.CV_32F) cv.MatchTemplate(I_s, ref_s, result, cv.CV_TM_CCOEFF_NORMED) # result = cv2.matchTemplate(img, refimg, cv2.TM_CCOEFF_NORMED) # result is a 'similarity' matrix, with values from -1.0 (?) to 1.0, # where 1.0 is most similar to the template image. result_np = np.asarray(result) match_flatidxs = np.arange(result_np.size)[ (result_np > confidence).flatten()] return [flatidx_to_pixelidx(flatidx, result_np.shape) for flatidx in match_flatidxs]
def fastResize(I, rszFac, sig=-1): if rszFac == 1: return I else: Icv = cv.fromarray(np.copy(I)) I1cv = cv.CreateMat(int(math.floor( I.shape[0] * rszFac)), int(math.floor(I.shape[1] * rszFac)), Icv.type) cv.Resize(Icv, I1cv) Iout = np.asarray(I1cv) if sig > 0: Iout = gaussian_filter(Iout, sig) return Iout
def analyzeimage(old_image_file): old_image = Image.open(old_image_file) old_image_arr = np.array(old_image) nir_img = np.array[2] green_img = np.array[1] - nir_img built_up_index = (green_img - 2 * nir_img) / (green_img + 2 * nir_img) built_up_image = cv2.fromarray(built_up_index) cv2.imwrite('static/temp.jpg', built_up_image) # analyze the image 'old_image' here # save the analyzed image to 'static/temp.jpg' # return the path of the new image file return "static/temp.jpg"
def img_from_buffer(buffer): np_arr = numpy.fromstring(buffer, 'uint8') np_mat = cv2.imdecode(np_arr, 0) return cv2.fromarray(np_mat)
rec.read("recognizer\\trainingData.yml") id=0 font=cv2.FONT_HERSHEY_SIMPLEX while(True): # Capture frame-by-frame ret, img = cam.read() # Our operations on the frame come here gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Detect faces in the image faces = faceDetect.detectMultiScale(gray,1.3,5) print("Found {0} faces!".format(len(faces))) # Draw a rectangle around the faces for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) id,conf=rec.predict(gray[y:y+h,x:x+w]) cv2.putText(cv2.fromarray(img),str(id),(x,y+h),font,255) # Display the resulting frame cv2.imshow('Face', img) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cam.release() cv2.destroyAllWindows()
recognizer.read('trainer/trainer.yml') cascadePath = "Classifiers/face.xml" faceCascade = cv2.CascadeClassifier(cascadePath) path = 'dataSet' cam = cv2.VideoCapture(0) fontFace = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 fontColor = (255, 255, 255) while True: ret, im = cam.read() gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE) for (x, y, w, h) in faces: nbr_predicted, conf = recognizer.predict(gray[y:y + h, x:x + w]) cv2.rectangle(im, (x - 50, y - 50), (x + w + 50, y + h + 50), (225, 0, 0), 2) if (nbr_predicted == 7): nbr_predicted = 'Obama' elif (nbr_predicted == 2): nbr_predicted = 'Anirban' cv2.PutText(cv2.fromarray(im), str(nbr_predicted) + "--" + str(conf), (x, y + h), font, 255) #Draw the text cv2.imshow('im', im) cv2.waitKey(10)
def detect(image): bitmap = cv2.fromarray(image)
import cv2 import numpy as np face_detect = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') eyes_detect = cv2.CascadeClassifier('haarcascade_eye.xml') cam = cv2.VideoCapture(0) rec = cv2.face.LBPHFaceRecognizer_create() rec.load('recognizer\\trainData.yml') id = 0 while (True): ret, img = cam.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_detect.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) id, conf = rec.predict(gray[y:y + h, x:x + w]) cv2.putText(cv2.fromarray(img), str(id), cv2.FONT_HERSHEY_PLAIN, (x, y + h), (0, 255, 0)) cv2.imshow('Face', img) if (cv2.waitKey() == ord('q')): break cam.release() cv2.destroyAllWindows()
import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('C:\Users\Rakib\Documents\messi5.jpg') r,g,b = cv2.split(img) img_bgr = cv2.merge([r,g,b]) b=cv2.fromarray(r) print b.shape #A =np.asanyarray(r) # print r.dtype # print r.shape # print type(r) #print A.shape # cv2.imshow('image',img_bgr) # cv2.imshow('image',img) # cv2.waitKey(0) # cv2.destroyAllWindows() #img = cv2.imread('messi5.jpg',0) # fig = plt.figure(1) plt.subplot(221),plt.imshow(r,'gray'),plt.title('Original') plt.subplot(222),plt.imshow(g,'gray'),plt.title('Original') plt.subplot(223),plt.imshow(b,'gray'),plt.title('Original') plt.subplot(224),plt.imshow(img_bgr,'gray'),plt.title('Reconstract') # #plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis # to remove x and y levels plt.show() # #How to add another plot # fig = plt.figure(5) # plt.subplot(121),plt.imshow(img,'gray'),plt.title('Original')