import numpy as np import cv2 cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) moments = cv2.moments(gray) huMoments = cv2.HuMoments(moments) ret, thresh = cv2.threshold(gray, 127, 255, 0) im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(frame, contours, -1, (0, 255, 0), 3) cv2.imshow('frame', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
src = cv.imread("./pictures/robot.jpg") cv.namedWindow("input1", cv.WINDOW_AUTOSIZE) cv.imshow("input1", src) src2 = cv.imread("./pictures/factory.jpg") cv.imshow("input2", src2) # 轮廓发现 contours1 = contours_info(src) contours2 = contours_info(src2) # 几何矩计算与hu矩计算 # opencv中提供了moments()来计算图像中的中心矩(最高到三阶), # HuMoments()用于由中心矩计算Hu矩. # 同时配合函数contourArea函数计算轮廓面积和arcLength来计算轮廓或曲线长度 mm2 = cv.moments(contours2[0]) hum2 = cv.HuMoments(mm2) # 轮廓匹配 for c in range(len(contours1)): mm = cv.moments(contours1[c]) hum = cv.HuMoments(mm) dist = cv.matchShapes(hum, hum2, cv.CONTOURS_MATCH_I1, 0) # matchShapes() 函数作用是比较两个形状 # double cvMatchShapes( const void* object1, const void* object2, # int method, double parameter=0 ); # object1 第一个轮廓或灰度图像 # object2 第二个轮廓或灰度图像 # method 比较方法,其中之一 CV_CONTOUR_MATCH_I1, CV_CONTOURS_MATCH_I2 or CV_CONTOURS_MATCH_I3 # parameter 比较方法的参数 (目前不用) if dist < 1: cv.drawContours(src, contours1, c, (0, 0, 255), 2, 8)
def huMonents(image): image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) return cv2.HuMoments(cv2.moments(image)).flatten()
def getMoments(self): mmnts = cv2.moments(self.mainContour) hummnts = cv2.HuMoments(mmnts).flatten() loghummnts = np.log(np.abs(hummnts)) return mmnts, hummnts, loghummnts
def externalCall(self): self.detected.data = False if self.inputContourIndexListName.data: contidx = self.inputContourIndexListName.data else: contidx = range(len(self.inputContourName.data)) image = self.inputImageName.data.copy() for cidx in contidx: cnt = self.inputContourName.data[cidx] mask = np.zeros(tuple(self.inputImageName.data.shape[:2]), np.uint8) cv2.drawContours(mask, self.inputContourName.data, -1, color=(250,250,50), thickness=-1) moments = cv2.moments(cnt) humoments = cv2.HuMoments(moments) try: centroid_x = moments['m10']/moments['m00'] centroid_y = moments['m01']/moments['m00'] except ZeroDivisionError: continue area = moments['m00'] equi_diameter = np.sqrt(4*area/np.pi) * 1.05 equi_radian = equi_diameter / 2.0 minhu = 1.59e-1 maxhu = 1.65e-1 # normal 1.64e-1 overhu = 1.70e-1 minarea = 2500 maxarea = 6000 quality = (humoments[0] - minhu) / (maxhu - minhu) color = (50, 250, 50) state = 'green' if humoments[0] > (maxhu) or humoments[0] < (minhu) or area < minarea or area > maxarea: color = (50, 50, 250) #continue state = 'red' self.detected.data = True if state == 'red': #continue pass #cv2.drawContours(image, self.inputContourName.data, -1, color=(250,250,50), thickness=1) #print '+++', state, gcorr, xm, 'sqrt ' + str(gcorr**2 - xm**2), '->', ym #self.scatter[int(ym*100)][int(xm*100)+100] += 0.14 #cv2.imshow('scatter', cv2.pyrUp(self.scatter)) #cv2.imwrite('data/scatterx.jpg', self.scatter) #self.drawText(image, '%.2f'%xm, int(xopt), int(centroid_y), scale=1, color=(225, 225, 225)) #self.drawText(image, '%.2f'%ym, int(centroid_x), image.shape[0]-10, scale=1, color=(225, 225, 225)) #cv2.line(image, (int(centroid_x), int(centroid_y)), (int(xopt), int(centroid_y)), (200,50,50), 1) #cv2.line(image, (int(centroid_x), int(centroid_y)), (int(centroid_x), image.shape[0]), (50,150,250), 1) #cv2.circle(image, (int(centroid_x), int(centroid_y)), int(equi_radian), (250, 250, 100), 1) #cv2.circle(image, (int(centroid_x), int(centroid_y)), 3, color=(255,50,50), thickness=-1) if True: #image[int(centroid_y-70-equi_radian):int(centroid_y-equi_radian-10), centroid_x-40:centroid_x+80] *= 0.45 #self.drawTextMarker(image, 'X: ' + str(round(centroid_x, 2)), centroid_x, centroid_y, equi_radian, 0, color) #self.drawTextMarker(image, 'Y: ' + str(round(centroid_y, 2)), centroid_x, centroid_y, equi_radian, 1, color) #self.drawTextMarker(image, 'Radian: ' + str(round(equi_radian, 2)), centroid_x, centroid_y, equi_radian, 1, color) self.drawTextMarker(image, 'HM0' + ': %.2e' % humoments[0], centroid_x, centroid_y, equi_radian, 2, color) self.drawTextMarker(image, 'Area' + ': %.2f' % area + 'm', centroid_x, centroid_y, equi_radian, 3, color) self.drawTextMarker(image, '' + ': %.2f' % (quality * 100) + '%', centroid_x, centroid_y, equi_radian, 4, color) cv2.imshow('xxx', image) crop = cv2.pyrUp(image) #crop = image[100:650, 550:1100] #crop = cv2.pyrUp(crop) cv2.imshow('crop', crop)
#cv2.drawContours(warped, [c], -1, (255,0,0), 3) #cv2.drawContours(hsv, [c], -1, (255,0,0), 3) #detect canny edges #paramters: source, threshold1, threshold2 edges = cv2.Canny(mask, 75, 150) #warped = four_point_transform(img, pts) #cv2.imshow("image", frame) #cv2.imshow("mask", mask) #cv2.imshow("contours", mask2) #cv2.imshow("gray", gray) #cv2.imshow("edges", edges) #cv2.imshow("hsv", hsv) retval = cv2.moments(mask2, binaryImage = True) hu = cv2.HuMoments(retval) #print retval lines = cv2.HoughLinesP(edges, 1, np.pi/180, 50, maxLineGap=50) avgLine = 0 numLines = 0 if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] #print line cv2.line(warped, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.circle(warped, (x1, y1), 5, (0, 255, 255), -1) #yellow first point cv2.circle(warped, (x2, y2), 5, (0, 0, 255), -1) #red second point radian_angle = math.atan2((x2-x1),(y1-y2)) #degree_angle = math.degrees(radian_angle) if radian_angle > (np.pi / 4):
upper_skin = np.array([125, 130, 255]) mask = cv2.inRange(hsv, lower_skin, upper_skin) mask = cv2.GaussianBlur(mask, (7, 7), 0) image, contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) if len(contours): cnt = max(contours, key=lambda x: cv2.contourArea(x)) hull = cv2.convexHull(cnt) drawing = np.zeros(crop_img.shape, np.uint8) epsilon = 0.03 * cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, epsilon, True) cv2.drawContours(drawing, [cnt], 0, (0, 255, 0), 0) cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 0) cv2.drawContours(drawing, [approx], 0, (0, 255, 0), 0) hu = cv2.HuMoments(cv2.moments(cnt))[:2] hu1 = round(hu[0][0] * 10, 2) hu2 = round(hu[1][0] * 1000, 2) L = cv2.arcLength(cnt, True) S = cv2.contourArea(cnt) LdivideS = round(S / L, 2) Lhull = cv2.arcLength(hull, True) leftmost = cnt[cnt[:, :, 0].argmin()][0][0] rightmost = cnt[cnt[:, :, 0].argmax()][0][0] BdivideLh = round((L - Lhull) / (rightmost - leftmost), 2) hull = cv2.convexHull(cnt, returnPoints=False) defects = cv2.convexityDefects(cnt, hull) count_concave = 0 count_convex = 0 longest = 0 shortest = 1000000
# USAGE # python humoments.py # import the necessary packages import cv2 # load the image, convert it to grayscale, and display it image = cv2.imread("test.png") image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("image", image) # extract Hu Moments from the image -- this list of numbers # is our 'feature vector' used to quantify the shape of the # object in our image features = cv2.HuMoments(cv2.moments(image)).flatten() print(features) # wait for a keypress cv2.waitKey(0)
cv2.imwrite(directory + "\\threshold.jpg", thresh) kernel = np.ones((5, 5), np.uint8) erode = cv2.erode(thresh, kernel) erode = cv2.cvtColor(erode, cv2.COLOR_BGR2GRAY) contours, hierarchy = cv2.findContours(erode, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.imwrite(directory + "\erode.jpg", erode) x, y, w, h = 0, 0, erode.shape[1]//2, erode.shape[0] left = erode[y:y+h, x:x+w] right = erode[y:y+h, x+w:x+w+w] left_pixels = cv2.countNonZero(left) right_pixels = cv2.countNonZero(right) ratio = -1 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) feature = cv2.HuMoments(cv2.moments(gray)).flatten() global_features.append(feature) if len(contours) > 0: contour = max(contours, key = cv2.contourArea) area = cv2.contourArea(contour) cv2.drawContours(segmented, contours, -1, (0, 255, 0), 3) cv2.imwrite(directory + "\contour.jpg", segmented) perimeter = cv2.arcLength(contour, True) if area > 0: if left_pixels == 0 and right_pixels > 0: labels.append("yes") elif right_pixels == 0 and left_pixels > 0: labels.append("yes") elif right_pixels > left_pixels: ratio = float(right_pixels/left_pixels) if ratio >= 1.5:
# Threshold image _,im = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY) #Calculate Central Moments M = cv2.moments(im) #calculates the value of the central moment cx = int(M['m10']/M['m00']) #gives x-coordinate of the central moment cy = int(M['m01']/M['m00']) #gives y-coordinate of the central moment print('Central Moments values: \n', M) print('\n') print('X-coordinate of the Central Moment value: ', cx) print('Y-coordinate of the Central Moment value: ', cy) print('\n') #Calculate Hu Moments huMoments = cv2.HuMoments(M) # empty list my_list = [] # Log scale hu moments for i in range(0,7): huMoments[i] = -1* copysign(1.0, huMoments[i]) * log10(abs(huMoments[i])) print('huM[',i+1,'] : ', huMoments[i]) my_list.append(huMoments[i]) df = pd.DataFrame(my_list) print(df) df.to_csv('data2.csv')
def ExtractFeatures(infolder, Id): Images = os.listdir(infolder) for image in Images: print(image) infilename = os.path.join(infolder, image) #Take Image input denoised_img = cv2.imread(infilename, 1) grayimg = cv2.cvtColor(denoised_img, cv2.COLOR_BGR2GRAY) #Threshold and Sobel Y operator filtering ret,thresh = cv2.threshold(grayimg, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) dilatekernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)) dilate = cv2.dilate(thresh, dilatekernel, iterations=1) retinakernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(39,39)) retinaonly = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, retinakernel) #Erode Image and Clean Image erosionkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) erosion = cv2.erode(thresh, erosionkernel, iterations=1) cleankernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) clean = cv2.morphologyEx(erosion, cv2.MORPH_OPEN, cleankernel) #Finding contours and ROI contourImage, contoursCV, hierarchy = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contourImage2, contoursCV2, hierarchy2 = cv2.findContours(retinaonly, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) sortedContours = sorted(contoursCV, key=cv2.contourArea, reverse=True) sortedContours2 = sorted(contoursCV2, key=cv2.contourArea, reverse=True) contour1y = sortedContours[0][sortedContours[0][:,:,1].argmax()] contour2y = [[0,0]] if len(sortedContours)==1 else sortedContours[1][sortedContours[1][:,:,1].argmax()] roi_contour_bottom = sortedContours[0] if contour1y[0][1]>=contour2y[0][1]: roi_contour_bottom = sortedContours[0] else: roi_contour_bottom = sortedContours[1] xarray = np.asarray(sortedContours2[0][:,:,0].flatten().tolist()) #print(xarray) yarray = np.asarray(sortedContours2[0][:,:,1].flatten().tolist()) #print(yarray) coeffs = coef(xarray, yarray) choroid_thickness = cv2.contourArea(roi_contour_bottom) retina_thickness = cv2.contourArea(sortedContours2[0]) humoments = cv2.HuMoments(cv2.moments(thresh)) eccentricity = region_props(grayimg).eccentricity() datadict = OrderedDict() datadict["type"] = Id datadict["choroid_thickness"] = choroid_thickness datadict["retina_thickness"] = retina_thickness datadict["eccentricity"] = eccentricity for i in range(0, len(humoments), 1): humoments[i] = -1 * copysign(1.0, humoments[i]) * log10(abs(humoments[i])) datadict["HuMoment_"+str(i+1)] = humoments[i][0] for i in range(1, 11, 1): datadict["coeff_"+str(i)] = coeffs[i] dictlist.append(datadict) print("Done extracting features from", image) dataframe = pd.DataFrame(dictlist) #dataframe.to_csv("test_features.csv") with open('train_features.csv', 'a') as f: dataframe.to_csv(f, header=False)
def MomentHU(image): #calul du moment de l'image img = cv2.imread(image, 0) HU = cv2.HuMoments(cv2.moments(img)).flatten() return HU # return le moment de hu de l'image
def getFeatures(grayROI, binaryROI, objContour): # features are first converted to lists which makes them easier to append # at the end of the function, they are converted to a numpy vector (1xN) featureList = [] #SHAPE # area area = cv2.contourArea(objContour) # aspect ratio ((x0, y0), (w, h), theta) = cv2.minAreaRect( objContour ) # from https://www.programcreek.com/python/example/89463/cv2.minAreaRect if w == 0 or h == 0: aspectRatio = 0 elif w >= h: aspectRatio = float(w) / h else: aspectRatio = float(h) / w # solidity hull = cv2.convexHull(objContour) hull_area = cv2.contourArea(hull) solidity = float(area) / hull_area # ellipse (xe, ye), (eMajor, eMinor), angle = cv2.fitEllipse(objContour) # contour contourLen = len(objContour) mean = np.mean(objContour) std = np.std(objContour) perimeter = cv2.arcLength(objContour, True) # circle (xc, yc), radius = cv2.minEnclosingCircle(objContour) # texture edgeIM = cv2.Canny(grayROI, 100, 200) # edge_detection onPix = np.sum(edgeIM) / 255 (h, w) = grayROI.shape texture = float(onPix / (w * h)) shape = [ area, aspectRatio, texture, solidity, eMajor, eMinor, contourLen, perimeter, radius, mean, std ] featureList.append(shape) # HU MOMENTS moments = cv2.moments(binaryROI) hu = cv2.HuMoments(moments) huMoments = -np.sign(hu) * np.log10(np.abs(hu)) huMoments = huMoments[:, 0] featureList.append(huMoments.tolist()) # ZERNIKE MOMENTS W, H = grayROI.shape R = min(W, H) / 2 z = zernike_moments(grayROI, R, 8) zsum = np.sum(z[1:]) z[0] = zsum # first Zernike moment always constant so replace with sum featureList.append(z.tolist()) # HARALICK FEATURES har = haralick(grayROI) haralickMean = har.mean(axis=0) featureList.append(haralickMean[1:].tolist()) # first entry is usually 0 # GRAYSCALE HISTOGRAM grayHist = np.zeros((5)) histogram = cv2.calcHist([grayROI], [0], None, [255], [0, 255]) grayHist[0] = histogram.mean() grayHist[1] = histogram.std() grayHist[2] = skew(histogram) grayHist[3] = kurtosis(histogram) histNorm = histogram / np.max(histogram) grayHist[4] = entropy(histNorm) featureList.append(grayHist.tolist()) # LOCAL BINARY PATTERNS eps = 1e-7 # so we don't divide by zero radius = 8 points = 8 * radius # Number of points to be considered as neighbourers lbp = feature.local_binary_pattern(grayROI, points, radius, method='uniform') # Uniform LBP is used (hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, points + 3), range=(0, points + 2)) hist = hist.astype("float") lbpHist = hist / (hist.sum() + eps) # normalize the histogram a = lbpHist[ 0: 6] # take the first 6 values and last 2 values, the rest are usually 0 b = lbpHist[-2:] c = np.concatenate((a, b)) featureList.append(c.tolist()) # convert featureList to featureVector (a numpy array 1xN) ff = featureList[0] + featureList[1] + featureList[2] + featureList[ 3] + featureList[4] + featureList[5] featureVector = np.array([ff]) #print('featureVector shape',featureVector.shape) return (featureVector)
def deteccion(): # capturo el video del telefono(1) o de la computadora(0) cap = cv.VideoCapture(1) capt = () # inicializo la variable para poder usarla momhu_positivo = [] while True: # leo la imagen de la camara y la comvierto a gris # despues de vuelta a RGB (display) para poder pintar en color _, image = cap.read() gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY) display = cv.cvtColor(gray, cv.COLOR_GRAY2RGB) # obtengo los valores de los trackbars threshold = cv.getTrackbarPos('Tresh', window_name) differencia = cv.getTrackbarPos('Diff', window_name) area_max = cv.getTrackbarPos('Area MAX', window_name) area_min = cv.getTrackbarPos('Area MIN', window_name) # aplico el threshold a la imagen _, thresh = cv.threshold(gray, threshold, 255, cv.THRESH_BINARY) # aplico las operaciones morfologicas kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (7, 7)) opening = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel) closing = cv.morphologyEx(opening, cv.MORPH_CLOSE, kernel) # Busco los contornos contornos, _ = cv.findContours(closing, cv.RETR_TREE, cv.CHAIN_APPROX_NONE) # flitro los contornos por tamaño de area contornos_filtrados = filtrar(contornos, area_max, area_min) # Cuando toco la tecla 'c' capturo el contorno mas grande y printeo sus momentos de hu if cv.waitKey(1) & 0xFF == ord('c'): capt = captura(contornos_filtrados) print('Momentos del contorno capturado') momentos = cv.moments(capt) mhc = cv.HuMoments(momentos) for n in range(0, 6): mhc[n] = -1 * copysign(1.0, mhc[n]) * log10(abs(mhc[n])) print(str(mhc[n])) # Recorro los contornos filtrados y los comapro con el capturado for cont in contornos_filtrados: if cv.matchShapes(capt, cont, cv.CONTOURS_MATCH_I2, 0) < (differencia / 100): # si el contorno es parecido lo dibujo en verde y guardo sus momentos de HU cv.drawContours(display, cont, -1, (0, 255, 0), 3) mom_positivo = cv.moments(cont) momhu_positivo = cv.HuMoments(mom_positivo) else: cv.drawContours( display, cont, -1, (0, 0, 255), 2) # si el contorno es distinto los dibujo en rojo cv.imshow(window_name, display) # Si aprieto la tecla 'H' muestro los momentos del contorno que es parecido if cv.waitKey(1) & 0xFF == ord('h'): print('Momentos del contorno comparado') for n in range(0, 6): momhu_positivo[n] = -1 * copysign( 1.0, momhu_positivo[n]) * log10(abs(momhu_positivo[n])) print(str(momhu_positivo[n])) # si aprieto la tecla 'q' termino el programa if cv.waitKey(1) & 0xFF == ord('q'): break
def shape(self,image): """Example function with types documented in the docstring. """ image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) feature = cv2.HuMoments(cv2.moments(image)).flatten() return feature
parser.add_argument("-C", "--C_parameter", help="Value for C parameter in linear SVM", required="True") args = vars(parser.parse_args()) c_value = float(args["C_parameter"]) # Ladataan opetusjoukko ja testijoukko with open('traindataset.pkl', 'rb') as f: traindata = pickle.load(f) with open('testdataset.pkl', 'rb') as f: testdata = pickle.load(f) # Otetaan ylös data ja luokat X_train = traindata[0] train_labels = traindata[1] X_train_features = [] X_test = testdata[0] test_labels = testdata[1] X_test_features = [] for i in range(X_train.shape[0]): X_train_features.append( log_function(cv2.HuMoments(cv2.moments(X_train[i])).flatten())) for i in range(X_test.shape[0]): X_test_features.append( log_function(cv2.HuMoments(cv2.moments(X_test[i])).flatten())) clf = svm.SVC(kernel='linear', C=c_value) clf.fit(X_train_features, train_labels) joblib.dump(clf, "model_linearsvm.pkl", compress=2) pdf = matplotlib.backends.backend_pdf.PdfPages("classification_report.pdf") accuracy(("Linear-SVM-Classifier", clf), X_test_features, test_labels, pdf) pdf.close()
def multiple_compare(path, mode=None, item=['crop', 'rotate', 'trans']): # 包含旋转+缩放,平移,裁剪的对比 dists_rs = [] dists_t_v = [] dists_t_h = [] dists_c = [] if mode == 'canny': img1 = cv2.imread(path, 0) img1 = cv2.Canny(img1, 100, 150) else: img1 = cv2.imread(path, 0) (h, w) = img1.shape[:2] center = (w / 2, h / 2) # -------------------- 旋转缩放 ---------------------------------------- ''' rotate: 0-359,间隔5 scale: 0.05-5,间隔0.1 ''' angle_range = 360 angle_interval = 5 scale_range = (0.05, 5.0) scale_interval = 0.1 if 'rotate' in item: for i in range(int(angle_range / angle_interval)): print('\rrotate & scale :{:.2f}% '.format( i * 100 / (angle_range / angle_interval)), end='') for j in range( int((scale_range[1] - scale_range[0]) / scale_interval)): M = cv2.getRotationMatrix2D( center, angle_interval * i, scale_range[0] + j * scale_interval) img2 = cv2.warpAffine(img1, M, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) dist_r = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_rs.append(dist_r) # cv2.imshow("pic",img2) # cv2.waitKey(0) x_angle = [ angle_interval * i for i in range(int(angle_range / angle_interval)) ] x_scale = [ scale_range[0] + j * scale_interval for j in range( int((scale_range[1] - scale_range[0]) / scale_interval)) ] draw_3D_point(x_angle, x_scale, dists_rs, x1label='Rotate', x2label='Scale', ylabel='dist') # -------------------------- 裁剪 ---------------------------------------- ''' 裁剪(水平和竖直):宽高为范围,其0.05作为slide步长 patch 大小设置8个档次:原图短边的0.1-0.8(正方形) ''' patch_range = (0.1, 1) patch_interval = 0.1 stride = 0.05 if 'crop' in item: label_list = [] # 存放打包的label名字 for c in range( int((patch_range[1] - patch_range[0]) / patch_interval) + 1): p = patch_range[0] + patch_interval * c dists_c_patch = [] patch_size = min(h, w) * p * patch_interval for y in range(int(1 / stride)): print('\rcrop :{:.2f}% '.format(y * 100 / ((1 / stride) * 8)), end='') for x in range(int(1 / stride)): y0 = int(y * 0.01 * h) x0 = int(x * 0.01 * w) y1 = int(min(h, patch_size + y0)) x1 = int(min(w, patch_size + x0)) img2 = img1[y0:y1, x0:x1] moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) dist_c = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_c_patch.append(dist_c) dists_c.append(dists_c_patch) # 一张图画多张小图,需要打包数据 # 打包label名,由于python计算机制导致float很长,取固定位小数 label_list.append('dist( patchsize: {} )'.format( Decimal(p).quantize(Decimal('0.0')))) x_pos = [i * 0.05 for i in range(int(1 / stride))] y_pos = [i * 0.05 for i in range(int(1 / stride))] draw_3D_point(x_pos, y_pos, dists_c, x1label='x1', x2label='x2', ylabel=label_list) # -------------------------- 平移 ---------------------------------------- ''' 水平和垂直四个方向的平移,两种策略:固定x还是固定y滑动 ''' trans_range = (0.05, 0.95) trans_interval = 0.05 x_trans = [ -trans_range[1] + trans_interval * i for i in range(2 * int((1 - trans_range[0]) / trans_interval)) ] for i in range(2 * int((1 - trans_range[0]) / trans_interval)): bias_h = (-trans_range[1] + trans_interval * i) * w print('\rtrans:{:.2f}%'.format( i * 100 / ((trans_range[1] - trans_range[0]) / trans_interval * 2), end='')) for j in range(2 * int((1 - trans_range[0]) / trans_interval)): bias_v = (-trans_range[1] + trans_interval * j) * h M = np.float32([[1, 0, bias_h], [0, 1, bias_v]]) # 平移矩阵M:[[1,0,x],[0,1,y]] img2 = cv2.warpAffine(img1, M, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) dist_t_v = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_t_v.append(dist_t_v) # cv2.imshow("pic",img2) # cv2.waitKey(0) for i in range(2 * int((1 - trans_range[0]) / trans_interval)): bias_h = (-trans_range[1] + trans_interval * i) * w for j in range(2 * int((1 - trans_range[0]) / trans_interval)): bias_v = (-trans_range[1] + trans_interval * j) * h M = np.float32([[1, 0, bias_v], [0, 1, bias_h]]) # 平移矩阵M:[[1,0,x],[0,1,y]] img2 = cv2.warpAffine(img1, M, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) dist_t_h = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_t_h.append(dist_t_h) # cv2.imshow("pic",img2) # cv2.waitKey(0) x = [i for i in range(2 * int((1 - trans_range[0]) / trans_interval))] y = [i for i in range(2 * int((1 - trans_range[0]) / trans_interval))] label_list = ['hori', 'vert'] data_list = [] data_list.append(dists_t_h) data_list.append(dists_t_v) draw_3D_point(x, y, data_list, x1label='x1', x2label='x2', ylabel=label_list) plt.show()
def extract_shape(im2): shape = cv2.HuMoments(cv2.moments(im2)) return shape.flatten()
def single_compare(path, mode=None): dists_r = [] dists_s = [] dists_t_h = [] dists_t_v = [] dists_c = [] if mode == 'canny': img1 = cv2.imread(path, 0) img1 = cv2.Canny(img1, 100, 150) else: img1 = cv2.imread(path, 0) (h, w) = img1.shape[:2] center = (w / 2, h / 2) # --------------------- 旋转:0-359度 --------------------- angle_range = 360 angle_interval = 1 x_angle = [i for i in range(int(angle_range / angle_interval))] for i in range(int(angle_range / angle_interval)): M = cv2.getRotationMatrix2D(center, i * angle_interval, 1.0) img2 = cv2.warpAffine(img1, M, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) # 同样建议取对数 moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) # 同样建议取对数 dist_r = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_r.append(dist_r) # cv2.imshow("pic",img2) # cv2.waitKey(0) # ---------------------- 缩放:0.05-10 ---------------------- scale_range = (0.05, 10.0) scale_interval = 0.01 x_scale = [ scale_range[0] + j * scale_interval for j in range(int((scale_range[1] - scale_range[0]) / scale_interval)) ] for j in range(int((scale_range[1] - scale_range[0]) / scale_interval)): M = cv2.getRotationMatrix2D(center, 0, scale_range[0] + j * scale_interval) img2 = cv2.warpAffine(img1, M, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) dist_s = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dists_s.append(dist_s) # cv2.imshow("pic",img2) # cv2.waitKey(0) # ------------------ 平移(水平方向和垂直方向):宽高的0.1-0.9,中间0.01为间隔---------------------- trans_range = (0.1, 0.9) trans_interval = 0.01 x_trans = [ -trans_range[1] + trans_interval * i for i in range(2 * int((1 - trans_range[0]) / trans_interval)) ] for i in range(2 * int((1 - trans_range[0]) / trans_interval)): bias_h = (-trans_range[1] + trans_interval * i) * w bias_v = (-trans_range[1] + trans_interval * i) * h M_h = np.float32([[1, 0, bias_h], [0, 1, 0]]) # 平移矩阵M:[[1,0,x],[0,1,y]] M_v = np.float32([[1, 0, 0], [0, 1, bias_v]]) # 平移矩阵M:[[1,0,x],[0,1,y]] img2 = cv2.warpAffine(img1, M_h, (w, h)) img3 = cv2.warpAffine(img1, M_v, (w, h)) moments1 = cv2.moments(img1) humoments1 = cv2.HuMoments(moments1) humoments1 = np.log(1e-16 + np.abs(humoments1)) moments2 = cv2.moments(img2) humoments2 = cv2.HuMoments(moments2) humoments2 = np.log(1e-16 + np.abs(humoments2)) moments3 = cv2.moments(img3) humoments3 = cv2.HuMoments(moments3) humoments3 = np.log(1e-16 + np.abs(humoments3)) dist_t_h = np.linalg.norm( humoments1.reshape(7, ) - humoments2.reshape(7, )) dist_t_v = np.linalg.norm( humoments1.reshape(7, ) - humoments3.reshape(7, )) dists_t_h.append(dist_t_h) dists_t_v.append(dist_t_v) # cv2.imshow("pic",img2) # cv2.waitKey(0) # --------------- 制图部分 ---------------------------- plt.figure(1) plt.subplot(2, 2, 1) plt.plot(x_angle, dists_r, color="r", linestyle="-", linewidth=1) plt.grid() plt.xlabel("x_angle") plt.ylabel("rotate") plt.figure(1) plt.subplot(2, 2, 2) plt.plot(x_scale, dists_s, color="b", linestyle="-", linewidth=1) plt.xlabel("x_scale") plt.ylabel("scale") plt.grid() plt.figure(1) plt.subplot(2, 2, 3) plt.plot(x_trans, dists_t_h, color="g", linestyle="-", linewidth=1) plt.grid() plt.xlabel("x_trans_h") plt.ylabel("trans_h") plt.figure(1) plt.subplot(2, 2, 4) plt.plot(x_trans, dists_t_v, color="g", linestyle="-", linewidth=1) plt.grid() plt.xlabel("x_trans_v") plt.ylabel("trans_v") plt.show()
def get_hu_moments(contour): moments = cv2.moments(contour) return cv2.HuMoments(moments)
def calc_parameters(img_org, show_params=False, show_hist=False, show_img=False): img_dst = np.copy(img_org) img_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY) img_sat = cv2.cvtColor(img_org, cv2.COLOR_BGR2HSV)[:, :, 1] # Processing img_blur = cv2.medianBlur(img_gray, 7) ret_binar, img_binar = cv2.threshold( img_blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) kernel_er = np.ones((3, 3), np.uint8) img_eroded = cv2.erode(img_binar, kernel_er, iterations=4) kernel_cl = np.ones((11, 11), np.uint8) img_closed = cv2.morphologyEx(img_eroded, cv2.MORPH_CLOSE, kernel_cl) kernel_grad = np.ones((3, 3), np.uint8) img_grad = cv2.morphologyEx(img_closed, cv2.MORPH_GRADIENT, kernel_grad) #opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) mask = cv2.bitwise_not(img_grad) img_masked = cv2.bitwise_and(img_dst, img_dst, mask=mask) # Find contours image, contours, hierarchy = cv2.findContours(img_grad, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Calculate area and perimeter max_area = 0 max_contour = None for c in contours: cont_area = cv2.contourArea(c) if cont_area > max_area: max_contour = c max_area = cont_area perimeter = cv2.arcLength(max_contour, True) (x, y), radius = cv2.minEnclosingCircle(max_contour) ellipse = cv2.fitEllipse(max_contour) axes = ellipse[1] minor_ell, major_ell = axes min_maj_ell_ratio = minor_ell / major_ell perimeter_ell = np.pi * (3 / 2 * (minor_ell + major_ell) - np.sqrt(minor_ell * major_ell)) perimeter_ratio = perimeter_ell / perimeter center = (int(x), int(y)) radius = int(radius) circle_area = np.pi * radius**2 circ_area_ratio = max_area / circle_area # Histogram hist_mask = np.zeros(img_org.shape) hist_mask = cv2.fillPoly(hist_mask, pts=[max_contour], color=(255, 255, 255)).astype(np.uint8)[:, :, 0] img_hist = cv2.bitwise_and(img_org, img_org, mask=hist_mask) color = ('b', 'g', 'r') histograms = [] for i, col in enumerate(color): hist = cv2.calcHist([img_org], [i], hist_mask, [256], [0, 256]) histograms.append(hist) # print(np.argmax(hist)) if show_hist: plt.plot(hist, color=col) plt.xlim([0, 256]) if show_hist: plt.show() img_hist_gray = cv2.cvtColor(img_org, cv2.COLOR_BGR2GRAY) img_hist_gray = cv2.bitwise_and(img_hist_gray, img_hist_gray, mask=hist_mask) hist = cv2.calcHist([img_hist_gray], [0], hist_mask, [256], [0, 256]) histograms.append(hist) if show_hist: plt.plot(hist, color='m') plt.xlim([0, 256]) plt.show() #data = histograms[1] #for d in data: #plt.hist(data, bins=60) hist_params = ['mean', 'var', 'skew', 'kurt'] hist_params_dict = {param: [] for param in hist_params} hue = cv2.cvtColor(img_hist, cv2.COLOR_BGR2HSV)[:, :, 0] hist_types = ['blue', 'green', 'red', 'gray', 'hue'] dataset = [ img_hist[:, :, 0], img_hist[:, :, 1], img_hist[:, :, 2], img_hist_gray, hue ] for hist_type, data in zip(hist_types, dataset): data = data[data > 0].ravel() hist_params_dict[f'{hist_params[0]}'].append(np.mean(data)) hist_params_dict[f'{hist_params[1]}'].append(np.var(data)) hist_params_dict[f'{hist_params[2]}'].append(skew(data)) hist_params_dict[f'{hist_params[3]}'].append(kurtosis(data)) params_dict = {} # Calculate hu moments retval = cv2.moments(img_closed) hu = cv2.HuMoments(retval, 7) for i in range(0, 7): hu[i] = -1 * math.copysign(1.0, hu[i]) * math.log10(abs(hu[i])) params_dict[f'hu{i}'] = float(hu[i]) params_dict['max_area'] = int(max_area) params_dict['circ_area_ratio'] = circ_area_ratio params_dict['perimeter'] = int(perimeter) params_dict['min_maj_ell_ratio'] = min_maj_ell_ratio params_dict['perimeter_ratio'] = perimeter_ratio for p in hist_params_dict: for i, colorscale in enumerate(hist_types): params_dict[p + '_' + colorscale] = hist_params_dict[p][i] if show_params: print('\t', ' '.join(f'{ht:6}' for ht in hist_types)) print( 'mean:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[0]}'])) print( 'var:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[1]}'])) print( 'skew:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[2]}'])) print( 'kurt:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[3]}'])) # print('kurt:\t', ' '.join('{:6.2f}'.format(p) for p in hist_params_dict[f'{hist_params[3]}'])) print('max contour area:', int(max_area)) print('max contour length:', int(perimeter)) print('circle area ratio:', int(circ_area_ratio * 100)) print('hu moments:') for i, h in enumerate(hu): print(f'\thu{i}: {h[0]}') if show_img: img_dst = cv2.ellipse(img_dst, ellipse, (0, 0, 255), 2) img_dst = cv2.circle(img_dst, center, radius, (255, 0, 0), 2) img_dst = cv2.drawContours(img_dst, contours, -1, (255, 255, 255), 2) # cv2.imshow('img_org',img_org) # cv2.imshow('binar', img_binar) # cv2.imshow('gradient', img_grad) # cv2.imshow('img_masked', img_masked) # cv2.imshow('closing', img_closed) # cv2.imshow('img_hist',img_hist) # cv2.imshow('img_hist_gray',img_hist_gray) # cv2.imshow('hist_mask',hist_mask) # cv2.imshow('img_dst',img_dst) dst = concatenate_images(img_blur, img_masked, hist_mask, img_binar, img_closed, img_dst) cv2.imshow(' ', dst) cv2.waitKey(0) # cv2.destroyAllWindows() return histograms, params_dict
def gen_features(video_img, bin_mask): # Output vector of features feat_vect = [] contour_vect = [] centroid_vect = [] all_feats = [] for i in range(0, len(video_img)): # Segmentation image bw_img = bin_mask[i] # Find contours ret, thresh = cv2.threshold(bw_img, 127, 255, 0) _, contours, _ = cv2.findContours(thresh, 1, 2) contour_vect.append(contours) # Feature vector for current image img_feats = np.empty([16, len(contours)]) centroid_contours = np.empty([2, len(contours)]) for c in range(0, len(contours)): cnt = np.squeeze(contours[c]) M = cv2.moments(cnt) # Centroid centroid = np.array([M['m10'] / M['m00'], M['m01'] / M['m00']]) centroid_contours[:, c] = centroid img_feats[0, c] = centroid[0] img_feats[1, c] = centroid[1] # Area img_feats[2, c] = cv2.contourArea(cnt) # Perimeter img_feats[3, c] = cv2.arcLength(cnt, True) # Calculate distances from centroid and circularity measures dist = np.sum((cnt - centroid)**2, axis=1) v11 = np.sum(np.prod(cnt - centroid, axis=1)) v02 = np.sum(np.square(cnt - centroid)[:, 1]) v20 = np.sum(np.square(cnt - centroid)[:, 0]) # Circularity m = 0.5 * (v02 + v20) n = 0.5 * np.sqrt(4 * v11**2 + (v20 - v02)**2) img_feats[4, c] = (m - n) / (m + n) # Min/max distance img_feats[5, c] = dist.min() img_feats[6, c] = dist.max() # Mean distance img_feats[7, c] = dist.mean() img_feats[8, c] = dist.std() img_feats[9:16, c] = cv2.HuMoments(M).flatten() feat_vect.append(img_feats) centroid_vect.append(centroid_contours) if i == 0: all_feats = img_feats else: all_feats = np.concatenate((all_feats, img_feats), axis=1) # Normalize features for i in range(0, len(feat_vect)): # NORMALIZATION ASSUMING GAUSSIAN DISTRIBUTION OF FEATS # mean_feats = np.tile(np.mean(all_feats, axis=1), (feat_vect[i].shape[1], 1)).T # std_feats = np.tile(np.std(all_feats, axis=1), (feat_vect[i].shape[1], 1)).T # feat_vect[i] = (feat_vect[i] - mean_feats)/std_feats # FEATURE SCALING min_feats = np.tile(np.min(all_feats, axis=1), (feat_vect[i].shape[1], 1)).T max_feats = np.tile(np.max(all_feats, axis=1), (feat_vect[i].shape[1], 1)).T feat_vect[i] = np.divide(np.subtract(feat_vect[i], min_feats), np.subtract(max_feats, min_feats)) return feat_vect, contour_vect, centroid_vect
#1 src = cv2.imread('./data/momentTest.jpg') gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) ret, bImage = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY) mode = cv2.RETR_EXTERNAL method = cv2.CHAIN_APPROX_SIMPLE image, contours, hierarchy = cv2.findContours(bImage, mode, method) dst = src.copy() cnt = contours[0] cv2.drawContours(dst, [cnt], 0, (255, 0, 0), 3) #2 M = cv2.moments(cnt) hu = cv2.HuMoments(M) print('hu.shape=', hu.shape) print('hu=', hu) #3 angle = 45.0 scale = 0.2 cx = M['m10'] / M['m00'] cy = M['m01'] / M['m00'] center = (cx, cy) t = (20, 30) A = cv2.getRotationMatrix2D(center, angle, scale) A[:, 2] += t # translation print('A=', A) # Affine 변환 cnt2 = cv2.transform(cnt, A) cv2.drawContours(dst, [cnt2], 0, (0, 255, 0), 3)
def logarit(image): image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) hu = cv2.HuMoments(cv2.moments(image)).flatten() log = list(map(lambda x: math.log(abs(x), math.exp(1)), hu)) return log
def fd_hu_moments(image): """Feature descriptor: Shape""" image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale feature = cv2.HuMoments(cv2.moments(image)).flatten() return feature
def shape_features(image): # Image need to be in grayscale first image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) shape_features = cv2.HuMoments(cv2.moments(image)).flatten() return shape_features
def fd_hu_moments(image): # feature-descriptor-1: Hu Moments image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) feature = cv2.HuMoments(cv2.moments(image)).flatten() return feature
def fd_hu_moments(image): image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) feature = cv2.HuMoments(cv2.moments(image)).flatten() return feature
def get_hu_moment_features(self, an_array): # This function seems not to provide predictable features!!! seven_invariants = cv2.HuMoments(cv2.moments(an_array)).flatten() return seven_invariants[0:2]