def drawCircleBoard(imgSize, boardSize, circleDis, circleRadius, originPt, backgroundColor, foregroundColor): Canvas = np.zeros(imgSize, dtype=np.uint8) Canvas[:, :] = backgroundColor RowNum, ColNum = boardSize OriginPoint_2x1 = np.array(originPt).reshape(2, 1) Circles_2xn = np.array([0, 0]).reshape(2, 1) for row in xrange(RowNum): for col in xrange(ColNum): x = col * circleDis y = row * circleDis Point = np.array([[x], [y]]) Circles_2xn = np.hstack((Circles_2xn, Point)) Circles_2xn = Circles_2xn[:, 1:] + OriginPoint_2x1 IPT.drawPoints(img=Canvas, pts_2xn=Circles_2xn, color=foregroundColor, radius=circleRadius) return Canvas
def OnMouse(event, x, y, flags, *args): global DownPoint global Roi_xyxy if cv2.EVENT_LBUTTONDOWN == event: DownPoint = [x, y] elif cv2.EVENT_LBUTTONUP == event: if DownPoint[0] != x and DownPoint[1] != y: Roi_xyxy[0:2] = [min(DownPoint[0], x), min(DownPoint[1], y)] Roi_xyxy[2:4] = [max(DownPoint[0], x), max(DownPoint[1], y)] print 'roi_xyxy:', Roi_xyxy print 'roi_xywh:', IPT.cvtRoi(roi=Roi_xyxy, flag=IPT.ROI_CVT_XYXY2XYWH) else: return
if __name__ == '__main__': Capture = CameraCapture(id=4) Capture.open(resolution=(1600, 1200), thread=False) # Capture.open(resolution=(1280, 720), thread=False) # Capture.open(resolution=(640, 480), thread=False) cv2.namedWindow("Src", cv2.WINDOW_NORMAL) cv2.setMouseCallback("Src", OnMouse) while True: SrcImg = Capture.takePhoto() if SrcImg is not None: # RotImg, RotMatrix = IPT.rotateImg(srcImg=SrcImg, angle_deg=90) # RotImg, RotMatrix = IPT.rotateImg(srcImg=SrcImg, angle_deg=-90) # RotGrayImg = cv2.cvtColor(src=RotImg, code=cv2.COLOR_BGR2GRAY) RotGrayImg = SrcImg IPT.drawRoi(img=RotGrayImg, roi=Roi_xyxy, roiType=IPT.ROI_TYPE_XYXY, color=(0, 0, 255)) cv2.imshow("Src", RotGrayImg) Key = chr(cv2.waitKey(30) & 0xff) if Key in ('q', 'Q'): print 'Params: ', Capture.getParams() print 'Frame rate: ', Capture.testFrameRate() Capture.release() break elif 'r' == Key: print 'roi_xyxy:', Roi_xyxy print 'roi_xywh:', IPT.cvtRoi(roi=Roi_xyxy, flag=IPT.ROI_CVT_XYXY2XYWH)
def addLogo(src, logoImg, pos=(0,0)): LogoShape = logoImg.shape Height, Width = LogoShape[:2] Roi_xywh = [pos[0], pos[1], Width, Height] _, RoiImg = IPT.getRoiImg(img=src, roi=Roi_xywh, roiType=IPT.ROI_TYPE_XYWH, copy=False) RoiImg[:] = cv2.addWeighted(RoiImg, 0.5, logoImg, 0.5, 0)
First = True SrcImg = cv2.imread('../../../Datas/AffineLena.png') SrcCanvas = np.zeros(SrcImg.shape, dtype=np.uint8) # cv2.namedWindow("Src", cv2.WINDOW_NORMAL) cv2.namedWindow("Src") cv2.setMouseCallback("Src", OnMouse1) cv2.namedWindow("Canvas") # cv2.namedWindow("Canvas", cv2.WINDOW_NORMAL) cv2.setMouseCallback("Canvas", OnMouse2) SrcPoints = np.float32([[150,123],[439,174],[148,380]]) CanvasPoints = np.float32([[0,0],[512,0],[0,512]]) while True: Img = SrcImg.copy() Canvas = SrcCanvas.copy() IPT.drawPoints(Img, np.array(LeftPoint).reshape(-1, 1), (0,0,255), radius=3) IPT.drawPoints(Canvas, np.array(RightPoint).reshape(-1, 1), (0,0,255), radius=3) for i in SrcPoints: IPT.drawPoints(Img, i.reshape(-1, 1), (0,255,0), radius=3) for i in CanvasPoints: IPT.drawPoints(Canvas, i.reshape(-1, 1), (0,255,0), radius=3) cv2.imshow('Src', Img) cv2.imshow('Canvas', Canvas) Key = chr(cv2.waitKey(30) & 255) if Key == 'l': if len(SrcPoints) < 3: SrcPoints.append(np.array(LeftPoint).reshape(-1)) elif Key == 'r': if len(CanvasPoints) < 3: CanvasPoints.append(np.array(RightPoint).reshape(-1))
Canvas = np.zeros(SrcImg.shape, dtype=np.uint8) Canvas[Corner] = RED Canvas[Edge] = GREEN Canvas[Flat] = BLUE # get corner points CornerImg = np.zeros(GrayImg.shape, dtype=np.uint8) CornerImg[Corner] = 255 cv2.imshow('CornerImg', CornerImg) CornerImg = cv2.morphologyEx(src=CornerImg, op=cv2.MORPH_CLOSE, kernel=np.ones((3, 3), dtype=np.uint8)) Contours, _ = cv2.findContours(image=CornerImg, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE) Corners_2xn = np.array([0, 0]).reshape(2, 1) for contour in Contours: CornerPt_2x1 = ContourAnalyst.getCentroid(contour=contour) Corners_2xn = np.hstack((Corners_2xn, CornerPt_2x1)) Corners_2xn = Corners_2xn[:, 1:] ShowImg = SrcImg.copy() IPT.drawPoints(img=ShowImg, pts_2xn=Corners_2xn, color=RED) # sub pixel IterationCounts = 100 EpsilonValue = 0.001 Criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, IterationCounts, EpsilonValue) CornersPts_nx1x2 = Corners_2xn.T.reshape(-1, 1, 2).astype(np.float32).copy() cv2.cornerSubPix(image=GrayImg, corners=CornersPts_nx1x2, winSize=(5, 5), zeroZone=(-1, -1), criteria=Criteria) print 'CornerNumber: ', CornersPts_nx1x2.shape[0] IPT.drawPoints(img=ShowImg, pts_2xn=CornersPts_nx1x2.T.reshape(2, -1), color=GREEN) cv2.namedWindow('SrcImg', cv2.WINDOW_NORMAL) cv2.imshow('Canvas', Canvas) cv2.imshow('SrcImg', ShowImg) cv2.waitKey()
CornerImg = np.zeros(GrayImg.shape, dtype=np.uint8) CornerImg[Corner] = 255 cv2.imshow('CornerImg', CornerImg) CornerImg = cv2.morphologyEx(src=CornerImg, op=cv2.MORPH_CLOSE, kernel=np.ones((3, 3), dtype=np.uint8)) Contours, _ = cv2.findContours(image=CornerImg, mode=cv2.RETR_EXTERNAL, method=cv2.CHAIN_APPROX_NONE) Corners_2xn = np.array([0, 0]).reshape(2, 1) for contour in Contours: CornerPt_2x1 = ContourAnalyst.getCentroid(contour=contour) Corners_2xn = np.hstack((Corners_2xn, CornerPt_2x1)) Corners_2xn = Corners_2xn[:, 1:] ShowImg = SrcImg.copy() IPT.drawPoints(img=ShowImg, pts_2xn=Corners_2xn, color=RED) # sub pixel IterationCounts = 100 EpsilonValue = 0.001 Criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS, IterationCounts, EpsilonValue) CornersPts_nx1x2 = Corners_2xn.T.reshape(-1, 1, 2).astype(np.float32).copy() cv2.cornerSubPix(image=GrayImg, corners=CornersPts_nx1x2, winSize=(5, 5), zeroZone=(-1, -1), criteria=Criteria) print 'CornerNumber: ', CornersPts_nx1x2.shape[0] IPT.drawPoints(img=ShowImg,
# elif '0' <= Key <= '9': # print 'Key: ', Key # Number = Key # -------------------------- match all --------------------------- # SrcImgPath = '../../../Datas/Numbers/OCR.png' SrcImg = cv2.imread(SrcImgPath) GrayImg = cv2.cvtColor(SrcImg, cv2.COLOR_BGR2GRAY) Number = '0' while True: TemplatePath = '../../../Datas/Numbers/' + Number + '.png' TemplateImg = cv2.imread(TemplatePath, 0) Height, Weight = TemplateImg.shape MatchResult = cv2.matchTemplate(image=GrayImg, templ=TemplateImg, method=cv2.TM_CCOEFF_NORMED) DrawImg = SrcImg.copy() Thresh = 0.8 Loc = np.where(MatchResult >= Thresh) for pt in zip(*Loc[:: -1]): Roi_xywh = [pt[0], pt[1], Weight, Height] IPT.drawRoi(img=DrawImg, roi=Roi_xywh, roiType=IPT.ROI_TYPE_XYWH, color=(0, 0, 255)) cv2.imshow('MatchResult', MatchResult) cv2.imshow('Result', DrawImg) Key = chr(cv2.waitKey() & 255) if Key == 'q': break elif '0' <= Key <= '9': print 'Key: ', Key Number = Key
UseTime = time.time() - T0 print '------------------------ SURF ------------------------ ' print 'Usetime:', UseTime print 'Keypoints: ', len(KeyPoints), 'Descriptions: ', Descriptions.shape SURFShowImg = cv2.drawKeypoints(image=GrayImg, keypoints=KeyPoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # ------------------- U-SURF ------------------- # useU_SURF = True U_SURF = cv2.SURF(hessianThreshold=HessianThreshold, nOctaves=GaussianPyramidNumber, nOctaveLayers=OctaveLayers, extended=DimDescription_64or128, upright=useU_SURF) T0 = time.time() KeyPoints, Descriptions = U_SURF.detectAndCompute(GrayImg, mask=None) UseTime = time.time() - T0 print '------------------------ U-SURF ------------------------ ' print 'Usetime:', UseTime print 'Keypoints: ', len(KeyPoints), 'Descriptions: ', Descriptions.shape USURFShowImg = cv2.drawKeypoints(image=GrayImg, keypoints=KeyPoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) FeaturePts_2xn = np.array([0, 0]).reshape(2, 1) for Pt in KeyPoints: x, y = Pt.pt FeaturePts_2xn = np.hstack((FeaturePts_2xn, np.array([x, y]).reshape(2, 1))) FeaturePts_2xn = FeaturePts_2xn[:, 1:] IPT.drawPoints(img=SrcImg, pts_2xn=FeaturePts_2xn, color=(0, 0, 255), radius=3) cv2.imshow('Src', SrcImg) cv2.imshow('SIFT', SIFTShowImg) cv2.imshow('U-SURF', USURFShowImg) cv2.imshow('SURF', SURFShowImg) cv2.waitKey()
Ratio = ModelHist / MatchHist Probability = Ratio[H.ravel(), S.ravel()] Probability = np.minimum(Probability, 1) Probability[Probability > thresh] = 255 Probability = Probability.reshape(MatchHSV.shape[:2]).astype(np.uint8) return Probability if __name__ == '__main__': cv2.namedWindow("Src", cv2.WINDOW_NORMAL) cv2.setMouseCallback("Src", OnMouse) while True: Img = cv2.imread('../../../Datas/Paper3.jpg') HSVImg = cv2.cvtColor(src=Img, code=cv2.COLOR_BGR2HSV) _, RoiImg = IPT.getRoiImg(img=Img, roi=Roi_xyxy, roiType=IPT.ROI_TYPE_XYXY, copy=True) RoiHist2D = calcHist2D(RoiImg) RoiHist2D = cv2.normalize(src=RoiHist2D, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) # DstImg = cv2.calcBackProject(images=[HSVImg], channels=[0, 1], hist=RoiHist2D, ranges=[0, 180, 0, 256], scale=1) DstImg = backProject_np(HSVImg, RoiImg) cv2.imshow('Back', DstImg) Disc = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(5, 5)) DstImg = cv2.filter2D(src=DstImg, ddepth=-1, kernel=Disc) cv2.imshow('Filter2D', DstImg)
nOctaves=GaussianPyramidNumber, nOctaveLayers=OctaveLayers, extended=DimDescription_64or128, upright=useU_SURF) T0 = time.time() KeyPoints, Descriptions = U_SURF.detectAndCompute(GrayImg, mask=None) UseTime = time.time() - T0 print '------------------------ U-SURF ------------------------ ' print 'Usetime:', UseTime print 'Keypoints: ', len(KeyPoints), 'Descriptions: ', Descriptions.shape USURFShowImg = cv2.drawKeypoints( image=GrayImg, keypoints=KeyPoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) FeaturePts_2xn = np.array([0, 0]).reshape(2, 1) for Pt in KeyPoints: x, y = Pt.pt FeaturePts_2xn = np.hstack( (FeaturePts_2xn, np.array([x, y]).reshape(2, 1))) FeaturePts_2xn = FeaturePts_2xn[:, 1:] IPT.drawPoints(img=SrcImg, pts_2xn=FeaturePts_2xn, color=(0, 0, 255), radius=3) cv2.imshow('Src', SrcImg) cv2.imshow('SIFT', SIFTShowImg) cv2.imshow('U-SURF', USURFShowImg) cv2.imshow('SURF', SURFShowImg) cv2.waitKey()
import numpy as np from Src.ImageProcessing.Contours.ContourAnalyst import ContourAnalyst import Src.ToolBox.ImageProcessTool as IPT RED = (0, 0, 255) BLUE = (255, 0, 0) GREEN = (0, 255, 0) if __name__ == '__main__': FilePath = '../../../Datas/Chessboard.jpg' # FilePath = '../../../Datas/Edge.png' SrcImg = cv2.imread(FilePath) GrayImg = cv2.cvtColor(src=SrcImg, code=cv2.COLOR_BGR2GRAY) # Corners_nx1x2 = cv2.goodFeaturesToTrack(image=GrayImg, maxCorners=54, qualityLevel=0.01, # minDistance=10, blockSize=5, useHarrisDetector=True, k=0.04) # ----------------------- Formula ----------------------- # # Shi-Tomasi Corner Detector # R = min(lambda_1, lambda_2) # ----------------------- Formula ----------------------- # Corners_nx1x2 = cv2.goodFeaturesToTrack(image=GrayImg, maxCorners=150, qualityLevel=0.01, minDistance=15) # Corners_nx1x2 = cv2.goodFeaturesToTrack(image=GrayImg, maxCorners=4, qualityLevel=0.01, minDistance=15) print 'Corners number: ', Corners_nx1x2.shape[0] Corners_2xn = Corners_nx1x2.T.reshape(2, -1) IPT.drawPoints(img=SrcImg, pts_2xn=Corners_2xn, color=RED, radius=3) cv2.imshow('Src', SrcImg) cv2.waitKey()
SrcImgPath = '../../../Datas/Numbers/OCR.png' SrcImg = cv2.imread(SrcImgPath) GrayImg = cv2.cvtColor(SrcImg, cv2.COLOR_BGR2GRAY) Number = '0' while True: TemplatePath = '../../../Datas/Numbers/' + Number + '.png' TemplateImg = cv2.imread(TemplatePath, 0) Height, Weight = TemplateImg.shape MatchResult = cv2.matchTemplate(image=GrayImg, templ=TemplateImg, method=cv2.TM_CCOEFF_NORMED) DrawImg = SrcImg.copy() Thresh = 0.8 Loc = np.where(MatchResult >= Thresh) for pt in zip(*Loc[::-1]): Roi_xywh = [pt[0], pt[1], Weight, Height] IPT.drawRoi(img=DrawImg, roi=Roi_xywh, roiType=IPT.ROI_TYPE_XYWH, color=(0, 0, 255)) cv2.imshow('MatchResult', MatchResult) cv2.imshow('Result', DrawImg) Key = chr(cv2.waitKey() & 255) if Key == 'q': break elif '0' <= Key <= '9': print 'Key: ', Key Number = Key
if 0 == flag: InterestedMask = np.where((mask==cv2.GC_BGD) + (mask==cv2.GC_PR_BGD), 255, 0).astype('uint8') elif 1 == flag: InterestedMask = np.where((mask==cv2.GC_FGD) + (mask==cv2.GC_PR_FGD), 255, 0).astype('uint8') GrabCutImg = cv2.bitwise_and(srcImg, srcImg, mask=InterestedMask) return GrabCutImg if __name__ == '__main__': ShowImg = SrcImg.copy() GrabCutMaskImg = np.zeros(SrcImg.shape, dtype=np.uint8) cv2.namedWindow('Src') cv2.setMouseCallback('Src', OnMouse) while True: ShowAllImg = DrawMaskImg.copy() if not np.allclose(Roi_xyxy, [0, 0, 0, 0]): IPT.drawRoi(img=ShowAllImg, roi=Roi_xyxy, roiType=IPT.ROI_TYPE_XYXY, color=(0, 255, 0)) cv2.imshow('Src', ShowAllImg) Key = chr(cv2.waitKey(15) & 255) if Key == 'q': break elif Key == '0': print " mark background regions with left mouse button \n" MaskType = cv2.GC_BGD elif Key == '1': print " mark foreground regions with left mouse button \n" MaskType = cv2.GC_FGD elif Key == '2': print " mark possible background regions with left mouse button \n" MaskType = cv2.GC_PR_BGD elif Key == '3':
H = matchImg[:, :, 0] S = matchImg[:, :, 1] Ratio = ModelHist / MatchHist Probability = Ratio[H.ravel(), S.ravel()] Probability = np.minimum(Probability, 1) Probability[Probability>thresh] = 255 Probability = Probability.reshape(MatchHSV.shape[:2]).astype(np.uint8) return Probability if __name__ == '__main__': cv2.namedWindow("Src", cv2.WINDOW_NORMAL) cv2.setMouseCallback("Src", OnMouse) while True: Img = cv2.imread('../../../Datas/Paper3.jpg') HSVImg = cv2.cvtColor(src=Img, code=cv2.COLOR_BGR2HSV) _, RoiImg = IPT.getRoiImg(img=Img, roi=Roi_xyxy, roiType=IPT.ROI_TYPE_XYXY, copy=True) RoiHist2D = calcHist2D(RoiImg) RoiHist2D = cv2.normalize(src=RoiHist2D, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) # DstImg = cv2.calcBackProject(images=[HSVImg], channels=[0, 1], hist=RoiHist2D, ranges=[0, 180, 0, 256], scale=1) DstImg = backProject_np(HSVImg, RoiImg) cv2.imshow('Back', DstImg) Disc = cv2.getStructuringElement(shape=cv2.MORPH_ELLIPSE, ksize=(5,5)) DstImg = cv2.filter2D(src=DstImg, ddepth=-1, kernel=Disc) cv2.imshow('Filter2D', DstImg) _, MaskImg = cv2.threshold(src=DstImg, thresh=50, maxval=255, type=cv2.THRESH_BINARY) cv2.imshow('Mask', MaskImg) Mask3D = cv2.merge((MaskImg, MaskImg, MaskImg)) ResultImg = cv2.bitwise_and(src1=Img, src2=Mask3D)