def test_computeDistance(self): a = self.get_sample('samples/data/shape_sample/1.png', cv.IMREAD_GRAYSCALE) b = self.get_sample('samples/data/shape_sample/2.png', cv.IMREAD_GRAYSCALE) _, ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) _, cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) hd = cv.createHausdorffDistanceExtractor() sd = cv.createShapeContextDistanceExtractor() d1 = hd.computeDistance(ca[0], cb[0]) d2 = sd.computeDistance(ca[0], cb[0]) self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor") self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor")
def test_computeDistance(self): a = cv.imread(os.path.join(MODULE_DIR, 'samples/data/shape_sample/1.png'), cv.IMREAD_GRAYSCALE) b = cv.imread(os.path.join(MODULE_DIR, 'samples/data/shape_sample/2.png'), cv.IMREAD_GRAYSCALE) if a is None or b is None: raise unittest.SkipTest("Missing files with test data") ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) hd = cv.createHausdorffDistanceExtractor() sd = cv.createShapeContextDistanceExtractor() d1 = hd.computeDistance(ca[0], cb[0]) d2 = sd.computeDistance(ca[0], cb[0]) self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor") self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor")
def hausdorff_distance(a_image, b_image): """ Measures the shape similarity between two (RGB colored) images. :return: The Hausdorff Distance (https://en.wikipedia.org/wiki/Hausdorff_distance) between two images. """ a_gray = cv2.cvtColor(a_image, cv2.COLOR_RGB2GRAY) ret_a, thresh_a = cv2.threshold(a_gray, 127, 255, 0) img_a, contours_a, hierarchy_a = cv2.findContours(thresh_a, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) b_gray = cv2.cvtColor(b_image, cv2.COLOR_RGB2GRAY) ret_b, thresh_b = cv2.threshold(b_gray, 127, 255, 0) img_b, contours_b, hierarchy_b = cv2.findContours(thresh_b, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) hd = cv2.createHausdorffDistanceExtractor() distance = hd.computeDistance(contours_a[0], contours_b[0]) return distance
def test_computeDistance(self): a = self.get_sample('samples/data/shape_sample/1.png', cv.IMREAD_GRAYSCALE) b = self.get_sample('samples/data/shape_sample/2.png', cv.IMREAD_GRAYSCALE) ca, _ = cv.findContours(a, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) cb, _ = cv.findContours(b, cv.RETR_CCOMP, cv.CHAIN_APPROX_TC89_KCOS) hd = cv.createHausdorffDistanceExtractor() sd = cv.createShapeContextDistanceExtractor() d1 = hd.computeDistance(ca[0], cb[0]) d2 = sd.computeDistance(ca[0], cb[0]) self.assertAlmostEqual(d1, 26.4196891785, 3, "HausdorffDistanceExtractor") self.assertAlmostEqual(d2, 0.25804194808, 3, "ShapeContextDistanceExtractor")
def Hausdorff(res_path, gt_path): res_list = os.listdir(res_path) haus = [] for i in range(len(res_list)): r_name = res_path + res_list[i] g_name = gt_path + res_list[i][:-4] + '.bmp' print(r_name, g_name) res = cv2.imread(r_name) h, w, _ = res.shape gt = cv2.imread(g_name) # 2.获取图片连通域 cnt_cs1 = get_contours(res) cnt_cs2 = get_contours(gt) # 3.创建计算距离对象 hausdorff_sd = cv2.createHausdorffDistanceExtractor() # 4.计算轮廓之间的距离 d1 = hausdorff_sd.computeDistance(cnt_cs1, cnt_cs2) haus.append(d1) return sum(haus) / len(haus)
YTrajectory.append(centroid_y) period.append(t[0]) frameID.append(int(0)) for i, frame in enumerate(f[1:]): i += 1 distances = [] NextFrame, _ = watershedsegmentation(frame) print("Number of Cells:{}".format(len(NextFrame))) print("CIN :{}".format(index)) for index2, NextFrameCell in enumerate(NextFrame): # structural similarity index for the images hd = cv2.createHausdorffDistanceExtractor() d1 = hd.computeDistance(refCont, NextFrameCell) if d1 is not None: distances.append(d1) ClosestCells.append(NextFrameCell) # print distances MinDistance = min(distances) MinIndex = distances.index(MinDistance) indexedCell = ClosestCells[MinIndex] M1 = cv2.moments(indexedCell) check = M1['m00'] if check != 0.0:
def setupHDComp(): global hd hd = cv2.createHausdorffDistanceExtractor()
from matplotlib import pyplot as plt gl.setConstant(1) gl.setContourPrecision(0.05) im1 = cv2.imread("chars/three.jpg", 0) #Import image as grayscale im1 = imutils.resize(im1, width=400) im1 = gl.blurImage(im1) im2 = cv2.imread("chars/three2.jpg", 0) im2 = imutils.resize(im2, width=400) im2 = gl.blurImage(im2) contour1, contour2, im1, im2 = gl.getBestContours(im1, im2) c1 = cv2.drawContours(im1.copy(), contour1, -1, (0, 255, 0), 3) c2 = cv2.drawContours(im2.copy(), contour2, -1, (0, 255, 0), 3) hd = cv2.createHausdorffDistanceExtractor() sd = cv2.createShapeContextDistanceExtractor() #order of c1, c2 does't matter (could be c2, c1) d1 = hd.computeDistance(contour1, contour2) d2 = sd.computeDistance(contour1, contour2) #TODO: Not working (0.0 every time) print(d1, " ", d2) cv2.imshow("magic", np.hstack([c1, c2])) if cv2.waitKey() & 0xFF == ord('q'): cv2.destroyAllWindows()
def extrai(path, identificador): color = cv2.imread(path, -1) color = cv2.resize(color, (0, 0), fx = 0.3, fy = 0.3) imgOriginal = color.copy() color = utils.removeSombras(color) utils.save('semSombra.jpg', color, id=identificador) #imgOriginal, color = recuperaAreaAssinada(color.copy(), imgOriginal, identificador) #utils.save('antesGray.jpg', color, id=identificador) imgGray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY) imgPbOriginal = imgGray.copy() utils.save('pb1.jpg', imgGray, id=identificador) #imgGray = rotate_bound(imgGray, 90) #utils.save('pb2.jpg', imgGray) #imgGray = cv2.blur(imgGray, (blurI, blurI)) #utils.save('blur.jpg', imgGray) utils.save('AntesThr.jpg', imgGray, id=identificador) imgGray, contours, hierarchy = extraiContornos(imgGray, identificador) utils.save('thr.jpg', imgGray, id=identificador) cnts2 = sorted(contours, key=functionSort, reverse=True)[0:5] printaContornoEncontrado(imgOriginal, cnts2, identificador) cnts2 = sorted(cnts2, key=functionSortPrimeiroPapel) printaOrdem(imgOriginal, cnts2, identificador) originalEmGray = cv2.cvtColor(imgOriginal, cv2.COLOR_BGR2GRAY) #originalHisto = cv2.equalizeHist(originalEmGray) originalHisto = originalEmGray lista = dict() cntArr = dict() #ratioDilatacao = recuperaRatioDilatacao(cnts2, imgPbOriginal, identificador) for i, c in enumerate(cnts2): x, y, w, h = cv2.boundingRect(c) b = 0 #print('{} x={} - y{}'.format(i,x,y)) utils.save('imgPbSemSombra2-{}.jpg'.format(i), imgPbOriginal, id=identificador) roi = imgPbOriginal[y-b:y + h+b, x-b:x + w+b] utils.save('roi_{}.jpg'.format(i), roi, id=identificador) #utils.save('_1_hist_{}.jpg'.format(i), roi) #roi = utils.resize(roi, width=300, height=300) resized = roi.copy() #resized = cv2.blur(resized, (blurI,blurI)) #utils.save('__{}_blur1.jpg'.format(i), resized) #resized = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) #resized = cv2.blur(resized, (5,5)) retval, resized = cv2.threshold(resized, 120, 255, type = cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) resized = utils.removeContornosPqnosImg(resized) utils.save('t_{}.jpg'.format(i), resized, id=identificador) cv2.waitKey(0) #print('ratioDilatacao ' + str(ratioDilatacao)) resized = utils.dilatation(resized, ratio=0.3) utils.save('t1_{}.jpg'.format(i), resized, id=identificador) im2, contours2, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print('Ajustando espacos') contours2, resized = ajustaEspacosContorno(contours2, resized) print('espacos ajustados') cnts = sorted(contours2, key=functionSort, reverse=True)[0] #roi = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) novaMat = np.zeros(roi.shape, dtype = "uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) #novaMat = cv2.resize(novaMat, (200,200), interpolation = cv2.INTER_AREA) #lista[i] = mahotas.features.zernike_moments(novaMat, 21) lista[i] = cnts cntArr[i] = cnts utils.save('_img_{}.jpg'.format(i), novaMat, id=identificador) #utils.show(color) hd = cv2.createHausdorffDistanceExtractor() sd = cv2.createShapeContextDistanceExtractor() out = "" sizeOut = "" resultadoApi = True imgResultado = imgOriginal.copy() for idx1 in range(0,1): #recupera apenas a primeira imagem e a compara com as outras item1 = lista[idx1] altura1, largura1 = calculaAlturaLargura(item1) soma = 0 for idx2 in range(0,5): item2 = lista[idx2] altura2, largura2 = calculaAlturaLargura(item2) #sizeOut += 'Altura {} - {} = {} / {}\n'.format(altura1, altura2, abs(altura1 - altura2), calcPercentual(largura1, largura2)) #sizeOut += 'Largura {} - {} = {} / {}\n'.format(largura1, largura2, abs(largura1 - largura2), calcPercentual(largura1, largura2)) sizeOut += 'Dimensao {} x {} \n'.format(largura2, altura2) tamanhoCompativel = alturaLarguraCompativel(altura1, largura1, altura2, largura2) #match = hd.computeDistance(item1, item2) #match = cv2.matchShapes(cntArr[idx1], cntArr[idx2], 1, 0.0) ida = sd.computeDistance(item1, item2) volta = sd.computeDistance(item2, item1) #ida = dist.euclidean(item1, item2) #volta = dist.euclidean(item2, item1) ida = round(ida, 5) volta = round(volta, 5) out += '{} vs {} ({}) == {} - {}\n'.format(idx1, idx2, tamanhoCompativel, ida, volta) #BGR if ( idx2 == 0 ): imgResultado = contorna(imgResultado, cnts2[idx2], (0,255,0)) #sucesso elif ( ida < 10 and volta < 10 and tamanhoCompativel == True): imgResultado = contorna(imgResultado, cnts2[idx2], (0,255,0)) #sucesso else: imgResultado = contorna(imgResultado, cnts2[idx2], (0,0,255)) #falha resultadoApi = False pathTxt = utils.buildPath(identificador, path="calc.txt") with open(pathTxt, "w") as text_file: text_file.write(sizeOut) text_file.write('\n') text_file.write(out) utils.save(names.RESULTADO, imgResultado, id=identificador) return resultadoApi
def extrai(path, pathCnh, identificador): paramsDb = db.select() valorAceitavel = paramsDb[1] valorAceitavelCnh = paramsDb[1] whTolerancia = paramsDb[2] pxWhiteTolerancia = paramsDb[3] paramsOut = """ PARAMETROS Tolerancia Pontos: {0} Tolerancia Pontos CNH: {1} Variacao no tamanho: {2}% Tolerancia densidade: {3}%\n """.format(valorAceitavel, valorAceitavelCnh, whTolerancia, pxWhiteTolerancia) densidadeOut = "" cnhColor = cv2.imread(pathCnh, -1) existeCnh = (cnhColor is not None) if (existeCnh == True): print("Existe") color = cv2.imread(path, -1) color = cv2.resize(color, (0, 0), fx=0.3, fy=0.3) imgOriginal = color.copy() color = utils.removeSombras(color) utils.save('semSombra.jpg', color, id=identificador) imgGray = cv2.cvtColor(color, cv2.COLOR_BGR2GRAY) imgPbOriginal = imgGray.copy() utils.save('pb1.jpg', imgGray, id=identificador) imgGray, contours, hierarchy = extraiContornos(imgGray, identificador) utils.save('thr.jpg', imgGray, id=identificador) cnts2 = sorted(contours, key=sortAltura, reverse=True) assinaturas = list() for i, c in enumerate(cnts2): x, y, w, h = cv2.boundingRect(c) existeEntre = existeEntreAlgumaFaixa(assinaturas, y, h) if existeEntre == False: assinaturas.append((y - 5, y + h + 5)) imgCopy = imgOriginal.copy() larguraImg = imgOriginal.shape[1] for ass in assinaturas: cv2.rectangle(imgCopy, (50, ass[0]), (larguraImg - 50, ass[1]), (255, 0, 0), 2) utils.save('identificadas_ass.jpg', imgCopy, id=identificador) if len(assinaturas) != 5: msgEx = "Numero de assinaturas encontradas ({}) é diferente do esperado (5)".format( len(assinaturas)) raise QtdeAssinaturasException(msgEx, identificador) assinaturas = sorted(assinaturas) lista = dict() #ratioDilatacao = recuperaRatioDilatacao(cnts2, imgPbOriginal, identificador) for i, ass in enumerate(assinaturas): roi = imgPbOriginal[ass[0]:ass[1], 0:larguraImg] utils.save('roi_{}.jpg'.format(i), roi, id=identificador) #roi = utils.resize(roi, width=300, height=300) resized = roi.copy() #resized = cv2.blur(resized, (blurI,blurI)) #utils.save('__{}_blur1.jpg'.format(i), resized) #resized = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY) #resized = cv2.blur(resized, (5,5)) retval, resized = cv2.threshold(resized, 120, 255, type=cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) utils.save('th_roi_{}.jpg'.format(i), resized, id=identificador) resized, densidade = utils.removeContornosPqnosImg(resized) utils.save('t_{}.jpg'.format(i), resized, id=identificador) #cv2.waitKey(0) #print('ratioDilatacao ' + str(ratioDilatacao)) #resized = utils.dilatation(resized, ratio=0.4) utils.save('t1_{}.jpg'.format(i), resized, id=identificador) im2, contours2, hierarchy = cv2.findContours(resized, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours2, resized = utils.ajustaEspacosContorno(contours2, resized) cnts = sorted(contours2, key=functionSort, reverse=True)[0] novaMat = np.zeros(roi.shape, dtype="uint8") cv2.drawContours(novaMat, [cnts], -1, 255, -1) xA, yA, wA, hA = cv2.boundingRect(cnts) square = novaMat[yA:yA + hA, xA:xA + wA] utils.save('square_{}.jpg'.format(i), square, id=identificador) #moment = mahotas.features.zernike_moments(square, 21) densidadeOut += "Densidade {} = {}\n".format(i, densidade) lista[i] = cnts, ass, square, densidade #utils.show(color) hd = cv2.createHausdorffDistanceExtractor() sd = cv2.createShapeContextDistanceExtractor() out = "" outCnh = "" sizeOut = "" resultadoApi = True imgResultado = imgOriginal.copy() percentCnh = [] for idx1 in range( 0, 1): #recupera apenas a primeira imagem e a compara com as outras item1 = lista[idx1][0] square1 = lista[idx1][2] dens1 = lista[idx1][3] altura1, largura1 = calculaAlturaLargura(item1) soma = 0 item1 = transformaItem(square1, altura1, largura1, identificador, idx1) itemCnh = None if (existeCnh == True): itemCnh, squareCnh = cnh.validaAssinaturaCnh( cnhColor, square1, identificador) itemCnh = transformaItem(squareCnh, altura1, largura1, identificador, 6) print("Contornos img_6 = " + str(len(itemCnh))) for idx2 in range(0, 5): print("Processando imagem " + str(idx2)) item2 = lista[idx2][0] ass = lista[idx2][1] square2 = lista[idx2][2] dens2 = lista[idx2][3] altura2, largura2 = calculaAlturaLargura(item2) sizeOut += 'Dimensao {} x {} \n'.format(largura2, altura2) tamanhoCompativel = alturaLarguraCompativel( altura1, largura1, altura2, largura2, whTolerancia) densidadeCompativel = calcDensidadeCompativel( dens1, dens2, pxWhiteTolerancia) item2 = transformaItem(square2, altura1, largura1, identificador, idx2) print("Contornos img_" + str(idx2) + " = " + str(len(item2))) #match = hd.computeDistance(item1, item2) if (idx1 != idx2): idaSD = round(sd.computeDistance(item1, item2), 5) voltaSD = round(sd.computeDistance(item2, item1), 5) idaHD = round(hd.computeDistance(item1, item2), 5) voltaHD = round(hd.computeDistance(item2, item1), 5) idaMM = calculaMoment(item1, idx1, item2, idx2, identificador) voltaMM = idaMM else: idaSD = 0 voltaSD = 0 idaHD = 0 voltaHD = 0 idaMM = 0 voltaMM = 0 if (existeCnh == True): idaCnh = round(sd.computeDistance(item2, itemCnh), 5) voltaCnh = round(sd.computeDistance(itemCnh, item2), 5) percentSimCnh = calculaSimilaridade(idaCnh, voltaCnh, valorAceitavelCnh) outCnh += '{} == {} - {} = {}%\n'.format( idx2, idaCnh, voltaCnh, percentSimCnh) percentCnh.append(percentSimCnh) #ida = dist.euclidean(item1, item2) #volta = dist.euclidean(item2, item1) out += '{} vs {} (T{}, D{}) \n'.format(idx1, idx2, tamanhoCompativel, densidadeCompativel) out += '----SD: {} - {} \n'.format(idaSD, voltaSD) out += '----HD: {} - {} \n'.format(idaHD, voltaHD) out += '----MH: {} - {} \n'.format(idaMM, voltaMM) #BGR if (idaSD < valorAceitavel and voltaSD < valorAceitavel and tamanhoCompativel == True and densidadeCompativel == True): imgResultado = contorna(imgResultado, larguraImg, ass, (0, 255, 0)) #sucesso else: imgResultado = contorna(imgResultado, larguraImg, ass, (0, 0, 255)) #falha resultadoApi = False pathTxt = utils.buildPath(identificador, path="calc.txt") with open(pathTxt, "w") as text_file: text_file.write(paramsOut) text_file.write('\n') text_file.write(sizeOut) text_file.write('\n') text_file.write(densidadeOut) text_file.write('\n') text_file.write(out) text_file.write('\n') text_file.write(outCnh) utils.save(names.RESULTADO, imgResultado, id=identificador) return { 'folhaAssinatura': resultadoApi, 'resultadoCnh': False, 'percentCnh': percentCnh }
#!/usr/bin/python # coding=utf-8 import cv2 as cv import time import numpy as np from utility.threshold import getTresholdByReExtrem from img_auto_canny_thresh import auto_canny import random as rng mysc = cv.createShapeContextDistanceExtractor() mync = cv.createHausdorffDistanceExtractor() def get_main_cnt(): """ 找到主要的轮廓 :return: """ def match_shape(f1, f2): img1 = cv.imread(f1, 0) img2 = cv.imread(f2, 0) # ########################################################use canny# ##################################### t1 = time.time() canny_thr1 = auto_canny(img1) query_canny = cv.Canny(img1, canny_thr1, canny_thr1 * 2, apertureSize=3) canny_thr2 = auto_canny(img2)
def run(self): if self.query_path == '': return sim_dict = {} sim_dict_flipped = {} img = cv_imread(self.query_path) resized_q = cv2.resize(img, (self.resize_w, self.resize_h)) if self.do_resize else img resized_qbinary = cv_toBinary(resized_q, self.do_smooth) self.img_query_group.clear() self.counter_query_group.clear() for i in range(360 // self.rot_angle): rot = cv_rotate(img, i * self.rot_angle) rot_flip = cv_rotate(cv2.flip(img, 1), i * self.rot_angle) resized_q = cv2.resize(rot, (self.resize_w, self.resize_h)) if self.do_resize else rot resized_qbinary = cv_toBinary(resized_q, self.do_smooth) resized_q_flip = cv2.resize( rot_flip, (self.resize_w, self.resize_h)) if self.do_resize else rot_flip resized_qbinary_flip = cv_toBinary(resized_q_flip, self.do_smooth) c0, _ = cv2.findContours(resized_qbinary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) cq = self._sample_contour_points(max( c0, key=len), self.sample_rate) if self.do_sample else max( c0, key=len) c0_flip, _ = cv2.findContours(resized_qbinary_flip, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) cq_flip = self._sample_contour_points( max(c0_flip, key=len), self.sample_rate) if self.do_sample else max(c0, key=len) self.img_query_group.append(rot) self.img_query_group.append(rot_flip) self.counter_query_group.append(cq) self.counter_query_group.append(cq_flip) if self.method == 'hu': for i, image_path in enumerate(self.img_paths): self.step_signal.emit(i + 1) sim = measure_img_sim.hu_moment_sim(self.query_path, image_path) sim_dict[image_path] = sim sorted_list = sorted(sim_dict.items(), key=lambda x: x[1]) elif self.method == 'emd': for image_path in self.img_paths: sim = measure_img_sim.earth_movers_distance( self.query_path, image_path) sim_dict[image_path] = sim sorted_list = sorted(sim_dict.items(), key=lambda x: x[1]) elif self.method == 'ssim': for image_path in self.img_paths: sim = measure_img_sim.structural_sim(self.query_path, image_path) sim_dict[image_path] = sim sorted_list = sorted(sim_dict.items(), key=lambda x: x[1], reverse=True) else: ''' Shape based methods ''' if self.method == 'idsc': dist = euclidean idsc = IDSC() query_descriptor = idsc.describe(resized_qbinary) for image_path in self.img_paths: print('comparing' + image_path) npy_path = image_path.replace('.jpg', '.npy') if not os.path.exists(npy_path): img_descriptor = idsc.describe(cv_toBinary(image_path), self.do_smooth) np.save(npy_path, img_descriptor) else: img_descriptor = np.load(npy_path) sim = dist(query_descriptor.flat, img_descriptor.flat) sim_dict[image_path] = sim sorted_list = sorted(sim_dict.items(), key=lambda x: x[1]) elif self.method == 'scd': scd = cv2.createShapeContextDistanceExtractor() scd.setRotationInvariant(self.rotation_invariant) for i, image_path in enumerate(self.img_paths): print(image_path, end=" ") self.step_signal.emit(i + 1) resized_mbinary = cv_toBinary( cv2.resize(cv_imread(image_path), (self.resize_w, self.resize_h)) if self.do_resize else cv_imread(image_path), self.do_smooth) blank = 255 - np.zeros(resized_mbinary.shape, np.uint8) c1, _ = cv2.findContours(resized_mbinary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) cm = self._sample_contour_points( max(c1, key=len), self.sample_rate) if self.do_sample else max(c1, key=len) if self.visualize: resized_mbinary = cv2.drawContours(cv2.cvtColor( resized_mbinary, cv2.COLOR_GRAY2BGR), [cm], 0, (0, 0, 255), thickness=1) cv2.imshow('aa', resized_mbinary) cv2.waitKey(1) if self.save_countours: print(cm.shape) save_path = image_path.replace('\\', '/') save_path = save_path.replace('轮廓图', 'counters') name = save_path.split('/')[-1] save_dir = save_path.replace(name, '') if not os.path.exists(save_dir): os.makedirs(save_dir) for i in cm: cv2.circle(blank, (i[0][0], i[0][1]), radius=1, color=(0, 0, 0), thickness=-1) # blank[int(i[0][1]), int(i[0][0])] = 0 cv2.imencode('.jpg', blank)[1].tofile(save_path) print(len(cm), end=" ") scd.setIterations(self.iterations) min_dist = sys.maxsize flipped = False rot_angle = 0 for i, cq in enumerate(self.counter_query_group): dist = scd.computeDistance(cq, cm) if dist < min_dist: min_dist = dist flipped = i % 2 == 1 rot_angle = i // 2 * self.rot_angle print(dist, "flip:", flipped, "rot_angle:", rot_angle) sim_dict[image_path] = (min_dist, flipped, rot_angle) sorted_list = sorted(sim_dict.items(), key=lambda x: x[1][0]) elif self.method == 'hausdorff': hsd = cv2.createHausdorffDistanceExtractor() for i, image_path in enumerate(self.img_paths): print(image_path) self.step_signal.emit(i + 1) resized_mbinary = cv_toBinary( cv2.resize(cv_imread(image_path), (self.resize_w, self.resize_h)) if self.do_resize else cv_imread(image_path), self.do_smooth) c1, _ = cv2.findContours(resized_mbinary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) cm = self._sample_contour_points( max(c1, key=len), self.sample_rate) if self.do_sample else max(c1, key=len) sim = hsd.computeDistance(cq, cm) sim_dict[image_path] = sim sorted_list = sorted(sim_dict.items(), key=lambda x: x[1]) self.done_signal.emit(sorted_list) self.quit()
def cv2_HDistance(gt, pred): import cv2 _, ca, _ = cv2.findContours(gt, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) _, ca, _ = cv2.findContours(pred, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS) hd = cv2.createHausdorffDistanceExtractor() return hd
def centroid_matching(self, newframes, oldframes, kernel, trackingMethd=''): # PERFORM SEGMENTATION USING WATERSHED ALGORITHM if trackingMethd == 'white': mask = white_background(oldframes) if trackingMethd == 'black': mask = black_background(oldframes) else: tkMessageBox.showinfo('Optical flow', 'defualt tracking is set to optical flow') # perform tracking for index, refCont in enumerate(mask): centroid = [] count = 0 M = cv2.moments(refCont) divisor = M['m00'] if divisor != 0.0: centroid_x = int(M['m10'] / divisor) # Get the x-centriod the cnt centroid_y = int(M['m01'] / divisor) # get the y-centriod the cnt XTrajectory.append(centroid_x) YTrajectory.append(centroid_y) period.append(t[0]) frameID.append(int(0)) for i, frame in enumerate(f[1:]): i += 1 distances = [] if trackingMethd == 'white': NextFrame = white_background(frame) if trackingMethd == 'black': NextFrame = black_background(frame) for index2, NextFrameCell in enumerate(NextFrame): # structural similarity index for the images hd = cv2.createHausdorffDistanceExtractor() d1 = hd.computeDistance(refCont, NextFrameCell) if d1 is not None: distances.append(d1) ClosestCells.append(NextFrameCell) # print distances MinDistance = min(distances) MinIndex = distances.index(MinDistance) indexedCell = ClosestCells[MinIndex] M1 = cv2.moments(indexedCell) check = M1['m00'] if check != 0.0: centroid_xx = int(M1['m10'] / check) # Get the x-centriod the cnt centroid_yy = int(M1['m01'] / check) # get the y-centriod the cnt # compared previous centroid if i is 1: previous_xx = centroid_xx previous_yy = centroid_yy AbsDifference = abs(previous_xx - centroid_xx) AbsDifference2 = abs(previous_yy - centroid_yy) # print AbsDifference try: while AbsDifference and AbsDifference2 > 3: MinDistance = min(distances) MinIndex = distances.index(MinDistance) indexedCell = ClosestCells[MinIndex] M1 = cv2.moments(indexedCell) check = M1['m00'] if check != 0.0: centroid_xx = int(M1['m10'] / check) # Get the x-centriod the cnt centroid_yy = int(M1['m01'] / check) # get the y-centriod the cnt AbsDifference = abs(previous_xx - centroid_xx) AbsDifference2 = abs(previous_yy - centroid_yy) if AbsDifference and AbsDifference2 <= 3: break del distances[MinIndex] del ClosestCells[MinIndex] except ValueError: pass if centroid_xx > previous_xx + 5 or centroid_yy > previous_yy + 5: continue previous_xx = centroid_xx print 'x', previous_xx previous_yy = centroid_yy print 'y', previous_yy # keep the trajectories of each cell refCont = NextFrameCell XTrajectory.append(centroid_xx) YTrajectory.append(centroid_yy) period.append(t[i]) frameID.append(int(i)) CID.append(int(index)) # free the distances distances = [] centroid_xx = None centroid_yy = None del distances centroid.append([frameID, CID, XTrajectory, YTrajectory, period]) xx = centroid[0] # free variables del previous_yy del previous_xx cellCentroid.append(xx) cc = cellCentroid[0] unpacked = zip(cc[0], cc[1], cc[2], cc[3], cc[4]) with open('Cancer_shape6.1_' + str(count) + '.csv', 'wt') as f1: writer = csv.writer(f1, lineterminator='\n') writer.writerow(('frameID', 'CellID', 'x-axis', "y-axis", 'time',)) for value in unpacked: writer.writerow(value) count += 1