import cv2 as cv import miscutils filename = r'D:\Users\me\Downloads\croppedcloud.jpg' resizeFactor = 0.2 cloud = cv.resize(cv.imread(filename), (0, 0), fx=resizeFactor, fy=resizeFactor) mapped = miscutils.brMap(cloud) thresh = miscutils.hytaThreshold(mapped) cv.imshow('threshold', thresh) cv.imshow('cloud', cloud) while (cv.waitKey() == 0): pass
#this code is bad import cv2 as cv import numpy as np import miscutils import random image = cv.imread( r"C:\Users\ktsun\AppData\Local\Programs\Python\Python38-32\cloud.jpg") cv.imshow('orig', image) #CHANGE AS NEEDED (do not set too high or else your computer will explode) radius = 1 threshold = 0.25 ret, image = cv.threshold(miscutils.brMap(image), threshold, 1, cv.THRESH_BINARY) image = np.array(image, dtype='float32') image = cv.cvtColor(image, cv.COLOR_GRAY2BGR) cv.imshow('thresh', image) for i in range(0, image.shape[0]): for j in range(0, image.shape[1]): pixel = image[i][j] if pixel[0] == 0: colorFound = False color = np.array([0, 0, 0]) vDist = 0 #vertical search for y in range(i - radius, i + radius): if y < 0 or y > image.shape[0]: continue
import miscutils import cv2 as cv import numpy as np import copy cloudOld = cv.resize(cv.imread(r'D:\Users\me\Downloads\alignedleft.jpg'),(0,0),fx=0.4,fy=0.4) cloudNew = cv.resize(cv.imread(r'D:\Users\me\Downloads\alignedright.jpg'),(0,0),fx=0.4,fy=0.4) mappedOld = miscutils.brMap(cloudOld) mappedNew = miscutils.brMap(cloudNew) thOld = miscutils.hytaThreshold(mappedOld) thNew = miscutils.hytaThreshold(mappedNew) grayOld = cv.cvtColor(cloudOld,cv.COLOR_BGR2GRAY) grayNew = cv.cvtColor(cloudNew,cv.COLOR_BGR2GRAY) pOld = cv.goodFeaturesToTrack(grayOld,100,0.3,7,mask=thOld) pNew,st,err = cv.calcOpticalFlowPyrLK(grayOld,grayNew,pOld,None) goodOld=pOld[st==1] goodNew=pNew[st==1] for i in range(len(goodOld)): cv.arrowedLine(cloudOld,tuple(goodOld[i]),tuple(goodNew[i]),(0,255,0), thickness=2) cv.imshow('optical flow', cloudOld) while(cv.waitKey())==0: pass cv.destroyAllWindows()
def binary(img, fixedThresh=None, sdthresh=None): return cv.threshold(img, fixedThresh, 255, cv.THRESH_BINARY_INV)[1] methods['binary'] = binary methods['otsu'] = kwargsWrapper( lambda img: cv.threshold(cv.GaussianBlur(img, (5, 5), 0), 0, 255, cv. THRESH_BINARY_INV + cv.THRESH_OTSU)[1]) methods['hybrid otsu'] = hybridFactory(methods['otsu']) methods['mce'] = kwargsWrapper(lambda img: cv.threshold( img, filters.threshold_li(img), 255, cv.THRESH_BINARY_INV)[1]) methods['hybrid mce'] = hybridFactory(methods['mce']) maps = {} maps['br'] = lambda x: np.array(miscutils.brMap(x) * 255, dtype='uint8') maps['saturation'] = lambda img: cv.cvtColor(img, cv.COLOR_BGR2HSV)[:, :, 1] params = {} params['br'] = {'fixedThresh': int(0.25 * 255), 'sdthresh': 0.03} params['saturation'] = {'fixedThresh': 29, 'sdthresh': 4.5} imDirectory = r'C:\Users\me\cloud-vision\threshtester\images' files = os.listdir(imDirectory) gtDirectory = r'C:\Users\me\cloud-vision\threshtester\2GT' results = { mapName: { methodName: {result: 0 for result in confusion} for methodName in methods } for mapName in maps }
import miscutils import cv2 as cv import numpy as np import copy import matplotlib.pyplot as plt cloud=cv.imread(r'C:\Users\ktsun\AppData\Local\Programs\Python\Python38-32\stuff\cloud.jpg') img=np.array(miscutils.brMap(cloud)*255,dtype='uint8') blur = cv.GaussianBlur(img,(5,5),0) ret,th = cv.threshold(blur,0,255,cv.THRESH_BINARY_INV+cv.THRESH_OTSU) contours, hier = cv.findContours(th, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) imContours = copy.deepcopy(cloud) cv.drawContours(imContours, contours, -1, (0,0,255),2) # removes contours which are children of other contours indices = filter(lambda x: hier[0][x][3] < 0, range(len(hier[0]))) contoursFiltered = list(map(lambda x: contours[x],indices)) imFiltered = copy.deepcopy(cloud) cv.drawContours(imFiltered, contoursFiltered, -1, (0,0,255),2) # finds bounding rectangles of filtered contours boundRects = list(map(lambda x: cv.boundingRect(x),contoursFiltered)) for rect in boundRects: cv.rectangle(cloud,(rect[0],rect[1]),(rect[0]+rect[2],rect[1]+rect[3]),(0,255,0),2) cv.imshow('rect',cloud) cv.imshow('contours',imContours) cv.imshow('filtered',imFiltered) ##following orb code from opencv docs img2 = cv.imread(r'C:\Users\ktsun\AppData\Local\Programs\Python\Python38-32\stuff\cloud.jpg',cv.IMREAD_GRAYSCALE) # trainImage for rect in boundRects: cloud=cv.imread(r'C:\Users\ktsun\AppData\Local\Programs\Python\Python38-32\stuff\cloud.jpg')
import copy import cv2 as cv import miscutils import numpy as np BLOCKROWS = 7 filenames = [ r'C:\Users\me\cloud-vision\examples\aligned1.jpg', r'C:\Users\me\cloud-vision\examples\aligned2.jpg', r'C:\Users\me\cloud-vision\examples\aligned3.jpg' ] ims = [ cv.resize(cv.imread(file), (0, 0), fx=0.5, fy=0.5) for file in filenames ] mapped = [miscutils.brMap(im) for im in ims] diff2 = mapped[2] - mapped[1] diff1 = mapped[1] - mapped[0] diff1 = np.array((diff1 - diff1.min()) / (diff1.max() - diff1.min()) * 255, dtype='uint8') diff2 = np.array((diff2 - diff2.min()) / (diff2.max() - diff2.min()) * 255, dtype='uint8') thresh1 = cv.threshold(diff1, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1] thresh2 = cv.threshold(diff2, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1] cv.imshow('thresh1', thresh1) cv.imshow('thresh2', thresh2) cv.waitKey(0) & 0xFF blockHeight = int(diff1.shape[0] / BLOCKROWS) blockWidth = int(diff1.shape[1] / BLOCKROWS) im1 = copy.deepcopy(ims[1]) for i in range(BLOCKROWS): (top, bottom) = tuple(np.array((i, i + 1)) * blockHeight) for j in range(BLOCKROWS):