frg = frags[index2Choose] relation = functionMisc.relatePoints( adjusted2[len(adjusted2) // 3], frg[0]) transdEdge = functionMisc.rotateEdgeToEdge( adjusted2[len(adjusted2) // 3], adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], frg) transdEdge2 = functionMisc.ratioTransfer( adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], transdEdge) if len(transdEdge2) == 0: fails = fails + 1 continue crossing = functionMisc.ecross( outerContour, transdEdge2) or (functionMisc.ecross( adjusted2[0:math.floor((len(adjusted2) // 3)) - 1:1], transdEdge2) or functionMisc.ecross( adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz) + 1:len(adjusted2):1], transdEdge2)) if not crossing: adjusted2 = np.concatenate( (np.concatenate( (adjusted2[0:math.floor((len(adjusted2) // 3)):1], transdEdge2)), adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz):len(adjusted2):1])) rtdd = functionMisc.createDescriptorSet( adjusted2, height, width)
def petsketch(img): height, width, channels = img.shape #img = cv2.fastNlMeansDenoisingColored(img,None,20,20,7,21) img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21) edges = cv2.Canny(img, 70, 200) kernel = np.ones((2, 2), np.uint8) dilation = cv2.dilate(edges, kernel, iterations=1) im3, Ocontours, hierarchy = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) c2 = [] forgotten = [] for cont in Ocontours: #print(len(cont)) if len(cont) > 10: c2.append(cont) elif len(cont) > 4: forgotten.append(cont[:len(cont) // 2]) contours = c2 #print(forgotten) # top = [] # bot = [] # left = [] # right = [] # for x in range(0,width): # top.append([x,0]) # bot.append([width-x,height]) # for y in range(0,height): # right.append([width,y]) # left.append([0,height-y]) # outerContour = top + right + bot+ left blank_image = np.zeros((height, width, 3), np.uint8) # im = cv2.imread("ex3.png") # #im = cv2.imread("circle2.png") # height, width, channels = im.shape # imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) # ret,thresh = cv2.threshold(imgray,127,255,0) # height, width, channels = im.shape # im3, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) # blank_image = np.zeros((height,width,3), np.uint8) # # startlistOfPoints = [] # # for ind, x in enumerate(contours[1]): # # xval = x[0][0] # # yval = x[0][1] # # startlistOfPoints.append([yval, xval]) # # listOfPoints= np.array(startlistOfPoints) # #blank_image = np.zeros(((height*4)//2,((width*4)//2),3), np.uint8) outerContour = [] for ind, x in enumerate(contours[0]): xval = x[0][0] yval = x[0][1] outerContour.append([yval, xval]) # for x in outerContour: # xval = x[1] # yval = x[0] # blank_image[yval][xval]=[0,255,255] folderName = "flatfish" frags = testFrags for cont in range(1, len(contours)): # print("onto ",cont," out of ",len(contours),len(contours[cont])) # sys.stdout.flush() startlistOfPoints = [] for ind, x in enumerate(contours[cont]): xval = x[0][0] yval = x[0][1] startlistOfPoints.append([yval, xval]) listOfPoints = np.array(startlistOfPoints) steps = 10 #maxAttempts = 1 adjusted2 = listOfPoints fails = 0 for x in range(0, steps): # selection = [] # distances = [] crossing = True while crossing: if fails > 0: fails = 0 break #historic = adjusted2 if (len(adjusted2) < 10): break elif (len(adjusted2) < 100): select = getRandomSection(len(adjusted2), len(adjusted2) // 3, len(adjusted2) // 6) else: select = getRandomSection(len(adjusted2), len(adjusted2) // 10, len(adjusted2) // 15) adjusted2 = adjustRoundList(adjusted2, math.floor(select[1]), math.floor(select[0])) sz = 1 if (select[0] > select[1]): sz = select[0] - select[1] else: sz = len(adjusted2) - select[1] + select[0] subfrag = adjusted2[math.floor(( len(adjusted2) // 3)):math.floor((len(adjusted2) // 3)) + math.floor(sz):1] subdesc = generateDescriptor(subfrag, height, width) myResult, myDistance = flann.nn_index(subdesc, 5, algorithm="kmeans", branching=32, iterations=7, checks=16) frg = frags[myResult[0][0]] relation = functionMisc.relatePoints( adjusted2[len(adjusted2) // 3], frg[0]) transdEdge = functionMisc.rotateEdgeToEdge( adjusted2[len(adjusted2) // 3], adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], frg) transdEdge2 = functionMisc.ratioTransfer( adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], transdEdge) if len(transdEdge2) == 0: fails = fails + 1 #print("lenfail length of frg ",len(frg), " sz ",sz) #break if fails < 1: #crossing = functionMisc.ecross(outerContour,transdEdge2) or (functionMisc.ecross(adjusted2[0 : math.floor((len(adjusted2)//3))-1 : 1],transdEdge2) or functionMisc.ecross(adjusted2[math.floor((len(adjusted2)//3))+math.floor(sz)+1 : len(adjusted2) : 1], transdEdge2)) #CAUSE WE NO LONGER CARE crossing = functionMisc.ecross(outerContour, transdEdge2) if not crossing: #transdEdge2 #adjusted2[0 : math.floor((len(adjusted2)//3)) : 1] partReplacing = adjusted2[math.floor(( len(adjusted2) // 3)):math.floor((len(adjusted2) // 3)) + math.floor(sz):1] newTransdEdge = [] # maxp = 5 # petx = random.randint(-maxp,maxp) * random.randint(-maxp,maxp) # pety = random.randint(-maxp,maxp) * random.randint(-maxp,maxp) petx = 0 pety = 0 for ind in range(0, len(transdEdge2)): p = transdEdge2[ind] q = partReplacing[(len(partReplacing) * ind) // len(transdEdge2)] newTransdEdge.append([ petx + q[0] - (-p[0] + q[0]) // 1, pety + q[1] - (-p[1] + q[1]) // 1 ]) transdEdge2 = newTransdEdge adjusted2 = np.concatenate( (np.concatenate( (adjusted2[0:math.floor((len(adjusted2) // 3)):1], transdEdge2)), adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz):len(adjusted2):1])) for x in adjusted2: xval = x[1] yval = x[0] blank_image[yval][xval] = [255, 255, 255] #print("fails: ",fails) for line in forgotten: for x in line: yval = x[0][1] xval = x[0][0] blank_image[yval][xval] = [255, 255, 255] # cv2.imshow('Edges',blank_image) # cv2.waitKey(0) # cv2.destroyAllWindows() return blank_image
def complete(): truths = 0 folderName = "flatfish" c = np.load("MPEG7dataset\\test\\extractions\\" + folderName + "\\metadata.npy") # just for dimensions im = cv2.imread('MPEG7dataset\\test\\apple\\apple-1.png') height, width, channels = im.shape blank_image = np.zeros(((height * 5) // 2, ((width * 5) // 2), 3), np.uint8) #finding most relevant in cow using cat as vs apples = np.load('MPEG7dataset\\test\\extractions\\apple\\Descriptors.npy') bell = np.load('MPEG7dataset\\test\\extractions\\bell\\Descriptors.npy') bone = np.load('MPEG7dataset\\test\\extractions\\Bone\\Descriptors.npy') car = np.load('MPEG7dataset\\test\\extractions\\car\\Descriptors.npy') carriage = np.load( 'MPEG7dataset\\test\\extractions\\carriage\\Descriptors.npy') cellphone = np.load( 'MPEG7dataset\\test\\extractions\\cellular_phone\\Descriptors.npy') child = np.load( 'MPEG7dataset\\test\\extractions\\children\\Descriptors.npy') chopper = np.load( 'MPEG7dataset\\test\\extractions\\chopper\\Descriptors.npy') face = np.load('MPEG7dataset\\test\\extractions\\face\\Descriptors.npy') flatfish = np.load( 'MPEG7dataset\\test\\extractions\\flatfish\\Descriptors.npy') fountain = np.load( 'MPEG7dataset\\test\\extractions\\fountain\\Descriptors.npy') keys = np.load('MPEG7dataset\\test\\extractions\\key\\Descriptors.npy') shoe = np.load('MPEG7dataset\\test\\extractions\\shoe\\Descriptors.npy') watch = np.load('MPEG7dataset\\test\\extractions\\watch\\Descriptors.npy') testImBig = np.concatenate([ apples, child, carriage, face, chopper, watch, bell, bone, car, cellphone, fountain, keys, shoe, flatfish ], axis=0) chosen = flatfish flann = FLANN() flann.build_index(testImBig, algorithm="kmeans", branching=32, iterations=7, checks=16) #relavantest stores the fragment indicies with the highest relevance, and their indicies relavantest = [] #looker is the bound on the length of relevantest looker = 30 ImgNo = 0 for imager in c: a = np.split(chosen, c[ImgNo])[1] #num is the index of the descriptor des relative to the lowest index of the image num = 0 for des in a: #print(len(a)) myResult, myDistance = flann.nn_index(des, 50, algorithm="kmeans", branching=32, iterations=7, checks=16) highestNonCow = len(testImBig) - len(chosen) inspectArray = [] for myR in range(len(myResult[0])): test = myResult[0][myR] - highestNonCow if test < 0: inspectArray = np.split(myResult[0], [0, myR])[1] break else: pass numNearerfound = 0 if len(inspectArray) > 1: dict = {'init': 'fake'} for cand in inspectArray: imgPlace = cand - (highestNonCow + 1) imgID = 0 for imageBound in c: if (imageBound[1] > imgPlace) and (imageBound[0] < imgPlace): imgID = imageBound[0] break if imgID in dict: pass else: dict[imgID] = 1 numNearerfound = numNearerfound + 1 myLower = c[ImgNo][0] tic = 0 inserted = False #just an ordered assertion into relavantest for pre in relavantest: if tic > looker: #if weve gone over the length of the thing #just unsyrence fir the deletion part later break if pre[0] < numNearerfound: #insertion at tic relavantest.insert(tic, ([numNearerfound, num + myLower])) inserted = True if len(relavantest) > looker: #to ensure we dont go over our specified length, we delete the last element del relavantest[len(relavantest) - 1] break tic = tic + 1 if inserted == False and len(relavantest) < looker: #initial filling of relecantest relavantest.append(([numNearerfound, num + myLower])) num = num + 1 ImgNo = ImgNo + 1 fragList = [] cowFrags = np.load('MPEG7dataset\\test\\extractions\\' + folderName + '\\Fragments.npy') for top in relavantest: number = top[1] fragList.append(cowFrags[number]) #phase 2=============================================================================================================================================================================== #================================================================================================================= #================================================================================================================= fullShape = AllRelGet() im = cv2.imread("exampleImage.png") height, width, channels = im.shape flann.build_index(testImBig, algorithm="kmeans", branching=32, iterations=7, checks=16) imgray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) ret, thresh = cv2.threshold(imgray, 127, 255, 0) height, width, channels = im.shape im3, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) blank_image = np.zeros((height, width, 3), np.uint8) c = np.load("MPEG7dataset\\test\\extractions\\" + folderName + "\\metadata.npy") outerContour = [] for ind, x in enumerate(contours[0]): xval = x[0][0] yval = x[0][1] outerContour.append([yval, xval]) for x in outerContour: xval = x[1] yval = x[0] blank_image[yval][xval] = [0, 255, 255] frags = fragList listOfPoints = fullShape steps = 0 maxAttempts = 5 adjusted2 = listOfPoints fails = 0 for x in range(0, steps): selection = [] distances = [] print("\n") for x in range(0, maxAttempts): crossing = True while crossing: if fails > 8: fails = 0 break historic = adjusted2 select = getRandomSection(len(adjusted2), len(adjusted2) // 12, len(adjusted2) // 20) adjusted2 = adjustRoundList(adjusted2, math.floor(select[1]), math.floor(select[0])) adjustedRot = adjusted2 #sz calculates from just select, could hapepn after the adjust round sz = 1 if (select[0] > select[1]): sz = select[0] - select[1] else: sz = len(adjusted2) - select[1] + select[0] #choose a random fragment index2Choose = math.floor((len(frags) * random.random()) // 1) frg = frags[index2Choose] relation = functionMisc.relatePoints( adjusted2[len(adjusted2) // 3], frg[0]) transdEdge = functionMisc.rotateEdgeToEdge( adjusted2[len(adjusted2) // 3], adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], frg) transdEdge2 = functionMisc.ratioTransfer( adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz)], transdEdge) if len(transdEdge2) == 0: fails = fails + 1 continue crossing = functionMisc.ecross( outerContour, transdEdge2) or (functionMisc.ecross( adjusted2[0:math.floor((len(adjusted2) // 3)) - 1:1], transdEdge2) or functionMisc.ecross( adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz) + 1:len(adjusted2):1], transdEdge2)) if not crossing: adjusted2 = np.concatenate( (np.concatenate( (adjusted2[0:math.floor((len(adjusted2) // 3)):1], transdEdge2)), adjusted2[math.floor((len(adjusted2) // 3)) + math.floor(sz):len(adjusted2):1])) targetLength = len(frg) rtdd = functionMisc.createDescriptorSet( adjusted2, height, width, targetLength, 2.2) totalSum = 0 for descr in rtdd: #======= replaced with full descriptor # myResult, myDistance = flann.nn_index(descr, 5, algorithm="kmeans", branching=32, iterations=7, checks=16) # intersum = 0 # for indist in myDistance[0]: # intersum = intersum + indist # totalSum = totalSum + intersum #============ #full relevance \/\/\/ #============= myResult, myDistance = flann.nn_index( descr, 50, algorithm="kmeans", branching=32, iterations=7, checks=16) highestNonCow = len(testImBig) - len(chosen) #the place in our index of the last noncow, the remainder of the index is cows //^^^ inspectArray = [] for myR in range(len(myResult[0])): test = myResult[0][myR] - highestNonCow if test < 0: inspectArray = np.split(myResult[0], [0, myR])[1] break else: pass numNearerfound = 0 if len(inspectArray) > 1: dict = {'init': 'fake'} for cand in inspectArray: imgPlace = cand - (highestNonCow + 1) imgID = 0 for imageBound in c: if (imageBound[1] > imgPlace) and ( imageBound[0] < imgPlace): imgID = imageBound[0] break if imgID in dict: pass else: dict[imgID] = 1 print("!") numNearerfound = numNearerfound + 1 if numNearerfound == 0: for myR in range(len(myResult[0])): test = myResult[0][myR] - highestNonCow if test > 0: inspectArray = np.split( myResult[0], [0, myR])[1] break else: pass if len(inspectArray) > 1: dict = {'init': 'fake'} for cand in inspectArray: imgPlace = cand - (highestNonCow + 1) imgID = 0 for imageBound in c: if (imageBound[1] > imgPlace) and ( imageBound[0] < imgPlace): imgID = imageBound[0] break if imgID in dict: pass else: dict[imgID] = 1 #print("!") numNearerfound = numNearerfound - 1 totalSum = totalSum - numNearerfound #============== selection.append(adjusted2) rtdd = functionMisc.createDescriptorSet( adjustedRot, height, width, targetLength, 2.2) totalSumOld = 0 for descr in rtdd: myResult, myDistance = flann.nn_index( descr, 50, algorithm="kmeans", branching=32, iterations=7, checks=16) highestNonCow = len(testImBig) - len(chosen) inspectArray = [] for myR in range(len(myResult[0])): test = myResult[0][myR] - highestNonCow if test < 0: inspectArray = np.split(myResult[0], [0, myR])[1] break else: pass numNearerfound = 0 if len(inspectArray) > 1: dict = {'init': 'fake'} for cand in inspectArray: imgPlace = cand - (highestNonCow + 1) imgID = 0 for imageBound in c: if (imageBound[1] > imgPlace) and ( imageBound[0] < imgPlace): imgID = imageBound[0] break if imgID in dict: pass else: dict[imgID] = 1 numNearerfound = numNearerfound + 1 if numNearerfound == 0: for myR in range(len(myResult[0])): test = myResult[0][myR] - highestNonCow if test > 0: inspectArray = np.split( myResult[0], [0, myR])[1] break else: pass if len(inspectArray) > 1: dict = {'init': 'fake'} for cand in inspectArray: imgPlace = cand - (highestNonCow + 1) imgID = 0 for imageBound in c: if (imageBound[1] > imgPlace) and ( imageBound[0] < imgPlace): imgID = imageBound[0] break if imgID in dict: pass else: dict[imgID] = 1 #print("!") numNearerfound = numNearerfound - 1 totalSumOld = totalSumOld - numNearerfound adjusted2 = historic print('NewNearer:', -totalSum, " OldNearer:", -totalSumOld) distances = distances + [totalSum - totalSumOld] indexI = 0 best = distances[0] for distInd in range(0, len(distances)): if (distances[distInd] < best): indexI = distInd best = distances[distInd] if (best < 0): adjusted2 = selection[indexI] print("tic") print("fails: ", fails) #================================================================ for x in fullShape: xval = x[1] yval = x[0] blank_image[yval][xval] = [255, 0, 0] for x in adjusted2: xval = x[1] yval = x[0] blank_image[yval][xval] = [255, 255, 255] cv2.imshow('Edges', blank_image) print("ok finish") print("truth", truths) plt.minorticks_on() plt.legend() plt.xlabel('x') plt.ylabel('y') plt.show() cv2.waitKey(0) cv2.destroyAllWindows()
def addFragment(fragments, links, nextFrag): indexToTest = 0 while (indexToTest < len(links)): removedLink = links[indexToTest] remShape = [] for x in range(indexToTest + 1, len(links)): remShape = remShape + fragments[x] + links[x] for x in range(0, indexToTest): remShape = remShape + fragments[x] + links[x] remShape = remShape + fragments[indexToTest] works = (not functionMisc.ecross(remShape, nextFrag)) if not works: indexToTest = indexToTest + 1 continue a = remShape[0] b = remShape[len(remShape) - 1] c = nextFrag[0] d = nextFrag[len(nextFrag) - 1] extra1 = functionMisc.fill([c, b]) extra1 = extra1[1:len(extra1) - 1:1] extra2 = functionMisc.fill([a, d]) extra2 = extra2[1:len(extra2) - 1:1] fullShape = remShape + extra1[::-1] + nextFrag + extra2[::-1] FirstFail = not ((not functionMisc.ecross(remShape, extra1)) and (not functionMisc.ecross(extra1, extra2)) and (not functionMisc.ecross(remShape, extra2)) and (not functionMisc.ecross(nextFrag, extra1)) and (not functionMisc.ecross(nextFrag, extra2))) if (not FirstFail): newFragments = fragments[:indexToTest + 1:1] + [ nextFrag ] + fragments[indexToTest + 1::1] newLinks = links[:indexToTest:1] + [extra1[::-1]] + [ extra2[::-1] ] + links[indexToTest + 1::1] return [True, newFragments, newLinks, fullShape] extra1 = functionMisc.fill([c, a]) extra1 = extra1[1:len(extra1) - 1:1] extra2 = functionMisc.fill([b, d]) extra2 = extra2[1:len(extra2) - 1:1] fullShape = remShape + extra2 + nextFrag[::-1] + extra1 SecondFail = not ((not functionMisc.ecross(remShape, extra1)) and (not functionMisc.ecross(extra1, extra2)) and (not functionMisc.ecross(remShape, extra2)) and (not functionMisc.ecross(nextFrag, extra1)) and (not functionMisc.ecross(nextFrag, extra2))) if (not SecondFail): newFragments = fragments[:indexToTest + 1:1] + [ nextFrag[::-1] ] + fragments[indexToTest + 1::1] newLinks = links[:indexToTest:1] + [extra2] + [ extra1 ] + links[indexToTest + 1::1] return [True, newFragments, newLinks, fullShape] indexToTest = indexToTest + 1 return [False]
# #NOT USED ANYWAYdstr = descrips[index2Choose] # frg = frags[index2Choose] relation = functionMisc.relatePoints( adjusted2[len(adjusted2)//3],frg[0]) transdEdge = functionMisc.rotateEdgeToEdge(adjusted2[len(adjusted2)//3],adjusted2[math.floor((len(adjusted2)//3))+math.floor(sz)], frg) transdEdge2 = functionMisc.ratioTransfer(adjusted2[math.floor((len(adjusted2)//3))+math.floor(sz)],transdEdge) if len(transdEdge2) == 0: fails = fails+1 print("WHYFAIL?????") break #crossing = functionMisc.ecross(outerContour,transdEdge2) or (functionMisc.ecross(adjusted2[0 : math.floor((len(adjusted2)//3))-1 : 1],transdEdge2) or functionMisc.ecross(adjusted2[math.floor((len(adjusted2)//3))+math.floor(sz)+1 : len(adjusted2) : 1], transdEdge2)) #CAUSE WE NO LONGER CARE crossing = functionMisc.ecross(outerContour,transdEdge2) if not crossing: adjusted2 = np.concatenate((np.concatenate((adjusted2[0 : math.floor((len(adjusted2)//3)) : 1],transdEdge2)),adjusted2[math.floor((len(adjusted2)//3))+math.floor(sz) : len(adjusted2) : 1])) # rtdd = functionMisc.createDescriptorSet(adjusted2,height,width) # totalSum = 0 # for descr in rtdd: # #======= replaced with full descriptor # # myResult, myDistance = flann.nn_index(descr, 5, algorithm="kmeans", branching=32, iterations=7, checks=16) # # intersum = 0 # # for indist in myDistance[0]: # # intersum = intersum + indist # # totalSum = totalSum + intersum # #============ # #full relevance \/\/\/ # #=============