def normalize(cls, img): """ cropping and zero-centering """ img = imutils.crop(img, std_size=(cls.config.width, cls.config.height)) img = (img - 128.0) / 255.0 return np.array([img])
def render_v2(data): img, predicted, title, description = preprocess(data) canvas = Image.open(ASSETS_TEMPLATE_DIR + '2.png') canvas.paste(imutils.crop(img, std_size=(983, 691), mode='PIL'), (48, 806)) border = Image.open(ASSETS_TEMPLATE_DIR + 'border.png') canvas.paste(border, (0, 0), mask=border) typewrite(canvas, title, (924, 290), font_size=107, align='right', font='msyhl') typewrite(canvas, description, (924, 460), font_size=50, align='right', font='msyhl', max_len=786) today = datetime.date.today().strftime('%b %d, %Y') typewrite(canvas, today, (916, 600), font_size=45, align='right', font='lishu') return imutils.pil2cv(canvas)
def render_v1(data): img, predicted, title, description = preprocess(data) canvas = Image.open(ASSETS_TEMPLATE_DIR + '1.png') canvas.paste(imutils.crop(img, std_size=(982, 982), mode='PIL'), (44, 49)) typewrite(canvas, title, (44, 1209), font_size=90) typewrite(canvas, description, (50, 1365), font_size=45, max_len=970) profile = Image.open(ASSETS_IMAGES_DIR + predicted + '.jpg') circlemask(profile, canvas, (688, 838), 180) return imutils.pil2cv(canvas)
def main(): Verbose = True # If operating on an existing image, load it from arguments and process data if 'pic' in sys.argv: color_chanels = imutils.split_channels(sys.argv[2], False) if Verbose: view_channel(color_chanels, 'c3') if '-manual' in sys.argv: test_area = imutils.define_area() else: # This area will crop the astronaut from buzzhole.jpg test_area = {'x1': 10, 'x2': 750, 'y1': 100, 'y2': 1100} img_slice = imutils.crop(canvas=color_chanels['c3'], area=test_area, show=False) # if verbose, show the new cropped image and it's dims if Verbose: print str(color_chanels['c1'].shape) + " Image Cropped to " + str( img_slice.shape) imutils.render_image(img_slice, 'Cropped ' + color_chanels['label'], False)
import numpy as np import argparse import imutils import cv2 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) cv2.imshow("Original", image) cropped = imutils.crop(image, x_area=(20, 50)) cv2.imshow("Horizontal Crop", cropped) cropped = imutils.crop(image, y_area=(20, 50)) cv2.imshow("Vertical Crop", cropped) cropped = imutils.crop(image, x_area=(20, 60), y_area=(20, 60)) cv2.imshow("Crop", cropped) cv2.waitKey(0)
def crop(self): self._make_dir() self._store_original() ratio = self.image.shape[0] / 1500.0 image = imutils.resize(self.image, 1500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 75, 200) (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5] # loop over the contours for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) # if our approximated contour has four points, then we # can assume that we have found our screen if len(approx) == 4: screenCnt = approx break coords = [] for dot in screenCnt: x = int(round(dot[0][0] * ratio)) y = int(round(dot[0][1] * ratio)) coords.append([[x, y]]) # cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) warped = transform.four_point_transform(image, screenCnt.reshape(4, 2)) # warped = transform.four_point_transform(self.image, screenCnt.reshape(4, 2) * ratio) # self._show(self.image, 'Warped') # warped = transform.four_point_transform(image, screenCnt.reshape(4, 2)) # convert the warped image to grayscale, then threshold it # to give it that 'black and white' paper effect # warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) # warped = threshold_adaptive(warped, 250, offset = 10) # warped = warped.astype("uint8") * 255 # import ipdb; ipdb.set_trace() height, width, x = warped.shape part_0 = imutils.crop(warped, 0, width*0.24, height * 0.267, height * 0.875) part_1 = imutils.crop(warped, width*0.24, width*0.46, height * 0.267, height * 0.875) part_2 = imutils.crop(warped, width*0.47, width*0.696, height * 0.267, height * 0.875) part_3 = imutils.crop(warped, width*0.696, width*0.968, height * 0.267, height * 0.875) cv2.imwrite(self._new_file('part_0.jpg'), part_0) cv2.imwrite(self._new_file('part_1.jpg'), part_1) cv2.imwrite(self._new_file('part_2.jpg'), part_2) cv2.imwrite(self._new_file('part_3.jpg'), part_3) return self.dir.split('/')[1]
import numpy as np import cv2 import argparse import imutils ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") args = vars(ap.parse_args()) image = cv2.imread(args["image"]) cv2.imshow("Original", image) print(image.shape) cropped = imutils.crop(image, 20, 150, 60, 170) cv2.imshow("Rapela's Face", cropped) cv2.waitKey(0)
y = [] image_ids = [] print "" counters = defaultdict(int) for i in range(len(image_names)): sys.stdout.write("\033[F") sys.stdout.write("\033[K") print "processing image... %.2f" % ( (1. + i) / len(image_names) * 100.) + "%" image_name = image_names[i] # image = (cv2.imread(image_name) - 128.0) / 255.0 image = cv2.imread(image_name) image = imutils.crop(image, (config.width, config.height)) cv2.imwrite('.debug/' + image_name.split('/')[-1], image) image = (image - 128.0) / 255.0 X.append(image) li = image_name.split("/") idx = int(li[3][0]) onehot = [0] * 7 onehot[idx] = 1 y.append(onehot) counters[idx] += 1 image_ids.append(image_name) print counters
import numpy as np import argparse import cv2 import imutils ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to the image") args = vars(ap.parse_args()) image = cv2.imread(args.get("image")) cv2.imshow("Original", image) cropped = imutils.crop(image, (0, 0), (300, 300)) cv2.imshow("Cropped", cropped) cv2.waitKey(0)
def main(): """ The Goal is, given images with a stack of 0, 1, or 4, orange rings, the program should be able to detect how many rings are in the stack. I attempt to detect using multiple methods, the results of which are printed at the end. """ #Initalize the variables to hold the results avgHueResult = -1 cntAspectRatioResults = -1 houghRotationAspectRatioResults = -1 #Get the path to the image of the rings ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required = True, help = "Path to the image") args = vars(ap.parse_args()) """ Note: The Photo Library I am using is very limited (very few teams have received the game elements), so the camera is at different angles in all the trials, but is especially different comparing the "no obstacles" images to the "w.obstacles" images. To demonstrate that my OpenCV algorithm which detects the average hue works, I use a different crop based on the boolean 'obstacles'. In the real robot game, the camera view is adjustable and should be much more consistent. """ obstacles = True if "no obstacles" in args["image"]: obstacles = False image = cv2.imread(args["image"]) #Get the image from the argument image = imutils.resize(image, newWidth = 720) #Ensure the image is 720 width so the crop will work # cv2.imshow("Original", image) original = image.copy() image = imutils.crop(image, (300, 220), (450, 330)) #Crop image to remove unecessary background noise cv2.imshow("Original Image Cropped", image) """ Detection Method 1: Isolate the ring stack, create a contour around it, then detect the height to width ratio of the contour to determine the number of rings """ edged = getEdges(image) #Get image with black background and edges cv2.imshow("Method 1: Image with background removed", edged) #get the contours #The Parameters are tuned such a maximum of 1 contour is returned for 1 or 4 rings, and 0 contours are returned for 0 rings (cnts, useless) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #cnts is a list of the actual contours cv2.drawContours(image, cnts, -1, (255, 120, 0), 2) #Drawing the contour cv2.imshow("Method 1: Contour Around Rings", image) #If there are no contours, there are 0 rings if len(cnts) == 0: cntAspectRatioResults = 0 else: (x, y, w, h) = cv2.boundingRect(cnts[0]) #returns tuple with rectangle dimensions to bound the contour c obj = image[y:y + h, x:x + w] #cropping the image #Based on the ration of width to height of the contour of the rings, determine the height of the stack if w > (2*h): cntAspectRatioResults = 1 else: cntAspectRatioResults = 4 print("# Rings Based on Width and Height of Contour: %d" % cntAspectRatioResults) # print("Contours in the image: {}".format(len(cnts))) cv2.waitKey(0) """ Detection Method 2: find the average hue in a cropped image of the stack, and compare them to threshold values to determine the ring stack """ imageCropped = imutils.crop(original, (340, 270), (420, 320)) #Crop image to remove background noise if obstacles: imageCropped = imutils.crop(original, (320, 250), (400, 300)) #Crop image to remove background noise cv2.imshow("Method 2: Cropped Image", imageCropped) imageCroppedIsolated = removeBackground(imageCropped) #color the background black to remove noise cv2.imshow("Method 2: Image with background removed", imageCroppedIsolated) imageHSVCroppedIsolated = cv2.cvtColor(imageCroppedIsolated, cv2.COLOR_BGR2HSV) #Converting to HSV cv2.imshow("Method 2: Image, cropped, and in HSV", imageHSVCroppedIsolated) hueTotal = 0 for r in range(0, imageHSVCroppedIsolated.shape[1] - 1): #Loop through the width of the img for c in range(0, imageHSVCroppedIsolated.shape[0] - 1): #Loop through the height of the img (hue, sat, val) = imageHSVCroppedIsolated[c, r] #Get HSV values of the pixel hueTotal = hueTotal + hue #Append to the total hue value hueAvg = hueTotal / (imageHSVCroppedIsolated.shape[1] * imageHSVCroppedIsolated.shape[0]) #Calculate the average hue print("avg Hue: %d" % hueAvg) #Based on the hue, determine the heigh of the ring stack based on pre-deterined thesholds if hueAvg >= 4: avgHueResult = 4 elif hueAvg >= 1: avgHueResult = 1 else: avgHueResult = 0 print("# Rings Based on Average Hue on HSV, Cropped, and Removed Background: %d" % avgHueResult) cv2.waitKey(0) """ Detection Method 3: Finding the contour around the ring stack, drawing it over a black background, then using Hough Line Detection to find the angle of the camera, then rotating the image based on the camera's tilt. Finally, compare the new height and width ratio of the contour and determine the number of rings. """ edged = getEdges(image) #Finding edges edgedNoLines = edged.copy() #Making a copy of the edged image for later use edged = cv2.GaussianBlur(edged, (5, 5), 0) #Blurring image for Improved Line Detection cv2.imshow("Method 3: Image edges: ", edged) #Last Parameter is Threshold, most be greater than threshold to be counted lines = cv2.HoughLinesP(edged, 1, np.pi/180, 60) #Detecting the lines # print(lines) try: #If there are no lines detected, the below code will throw an exception, so I add a catch block horizSlopesSum = 0.0 numHorizSlopes = 0 for line in lines: #Draw the lines found x1, y1, x2, y2 = line[0] #Get endpoints of line cv2.line(edged, (x1, y1), (x2, y2), (255, 255, 255), 2) #Draw Line if x2 - x1 != 0: #Ensure we're not dividing by zero slope = (y2-y1) / (x2-x1) if abs(0.0 - slope) < 0.25: #If the slope is less than 0.25, then we consider it a line that's supposed to be horizontal horizSlopesSum += slope numHorizSlopes += 1 avgHorizSlope = horizSlopesSum / numHorizSlopes #Finding the avg slope of the lines that are supposed to be horizontal # print("Slope: %f" % avgHorizSlope) angle = math.degrees(math.atan(avgHorizSlope)) #Convert slope to an angle for rotation # print("angle: %f" % angle) cv2.imshow("Method 3: Lines Detected from Hough Line Detection", edged) rotated = imutils.rotate(edgedNoLines, angle) #Rotating the Image cv2.imshow("Method 3: Edges rotated based on Angle of Lines", rotated) #get the contours #The Parameters are tuned such a maximum of 1 contour is returned for 1 or 4 rings, and 0 contours are returned for 0 rings (cnts, useless) = cv2.findContours(rotated.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #cnts is a list of the actual contours cv2.drawContours(image, cnts, -1, (255, 120, 0), 2) #Drawing the contour cv2.imshow("Method 3: Contour Around Rotated Image", image) #If there are no contours, there are 0 rings if len(cnts) == 0: houghRotationAspectRatioResults = 0 else: (x, y, w, h) = cv2.boundingRect(cnts[0]) #returns tuple with rectangle dimensions to bound the contour c obj = image[y:y + h, x:x + w] #cropping the image #Based on the ration of width to height of the contour of the rings, determine the height of the stack if w > (2*h): houghRotationAspectRatioResults = 1 else: houghRotationAspectRatioResults = 4 except: #Catch in case 'lines' is None (no lines detected) print("Exception Thrown") houghRotationAspectRatioResults = 0 print("# Rings based on rotated image and detecting aspect ratio: %d" % houghRotationAspectRatioResults) cv2.waitKey(0)
ours_joints_3d[:, :, : 3] = ours_joints_3d[:, :, : 3] - ours_pelvis #centering ours_vertices = ours_vertices - ours_pelvis # spin_model_pelvis = (spin_joints_3d[:, 27:28,:3] + spin_joints_3d[:,28:29,:3]) / 2 # spin_joints_3d[:,:,:3] = spin_joints_3d[:,:,:3] - spin_model_pelvis #centering # spin_vertices = spin_vertices - spin_model_pelvis # if ii %100==0: # bDraw =True if bDraw: ours_vertices = ours_output.vertices.d().numpy() rawImg = cv2.imread(imgFullPath) croppedImg = crop(rawImg, center, scale, [constants.IMG_RES, constants.IMG_RES]) rawImg = viewer2D.Vis_Skeleton_2D_SPIN49( data['keypoint2d'][0][:, :2], pt2d_visibility=data['keypoint2d'][0][:, 2], image=rawImg) #Visualize image viewer2D.ImShow(rawImg, name='rawImg') viewer2D.ImShow(croppedImg, name='croppedImg') b = 0 ############### Visualize Mesh ############### pred_vert_vis = ours_vertices[b] pred_vert_vis *= pred_camera_vis[b, 0] pred_vert_vis[:, 0] += pred_camera_vis[ b,
def ocrSetNumber(inputImage): referenceImage = "/Users/alexanderwarnes/Desktop/PokeScan-Photos/CardFakingResources/PokemonNumbers/Gill-Std-Condensed.jpg" # Get reference images to compare to ref = cv2.imread(referenceImage) # cv2.imshow('f**k you c**t', ref) # cv2.waitKey(0) ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY) ref = cv2.threshold(ref, 10, 255, cv2.THRESH_BINARY_INV)[1] refCnts = cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) refCnts = refCnts[0] if imutils.is_cv2() else refCnts[1] refCnts = contours.sort_contours(refCnts, method="left-to-right")[0] digits = dict() for (i, c) in enumerate(refCnts): (x, y, w, h) = cv2.boundingRect(c) roi = ref[y-1:y+h + 1, x-1:x+w + 1] if i == 0 else ref[y:y+h, x:x+w] roi = cv2.resize(roi, (57, 88)) if (i == 9): i = -1 digits[i + 1] = roi # for (index, image) in digits.items(): # cv2.imshow(f"number{index}", image) # Set up image to compare to. rectKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (30, 10)) sqKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15)) # print(inputImage) image = cv2.imread(inputImage) # cv2.namedWindow("first", cv2.WINDOW_NORMAL) # cv2.imshow("first", image) # image = imutils.resize(image, 1000) original = image.copy() gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (9, 9), 0) blackhat = cv2.morphologyEx(blurred, cv2.MORPH_BLACKHAT, rectKernel) gradX = cv2.Sobel(blackhat, cv2.CV_64F, dx=1, dy=0, ksize=-1) # cv2.namedWindow('thresh', cv2.WINDOW_NORMAL) # cv2.imshow('thresh', gradX) # cv2.waitKey(0) gradX = np.absolute(gradX) (minVal, maxVal) = (np.min(gradX), np.max(gradX)) gradX = (255 * ((gradX - minVal) / (maxVal - minVal))) gradX = gradX.astype("uint8") gradX = cv2.morphologyEx(gradX, cv2.MORPH_CLOSE, rectKernel) thresh = cv2.threshold(gradX, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1] thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, sqKernel) cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] locs = list() for (i, c) in enumerate(cnts): (x, y, w, h) = cv2.boundingRect(c) ar = w / float(h) # cv2.drawContours(original, [c], -1, (0, 0, 255), 1) # 2900, 3250, 1900, 2250 if (1900 < x < 2250 and 2900 < y < 3250) or (100 < x < 500 and 2900 < y < 3250): # if 3.0 < ar < 7.0: if (90 < w < 220) and (10 < h < 45): # cv2.drawContours(original, [c], -1, (0, 255, 0), 1) locs.append((x, y, w, h)) locs = sorted(locs, key=lambda x:x[0]) output = list() # print(locs) for (i, (gX, gY, gW, gH)) in enumerate(locs): groupOutput = list() group = gray[gY - 5:gY + gH + 5, gX - 5:gX + gW + 5] group = cv2.threshold(group, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] # cv2.imshow('group', group) digitCnts = cv2.findContours(group.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE) digitCnts = digitCnts[0] if imutils.is_cv2() else digitCnts[1] digitCnts = contours.sort_contours(digitCnts)[0] digitCnts = [cnt for cnt in digitCnts[1:] if cv2.contourArea(cnt) > 75] # cv2.drawContours(original, digitCnts, -1, (0, 0, 255), 1) # cv2.imshow('cnts', original) for (i, c) in enumerate(digitCnts): (x, y, w, h) = cv2.boundingRect(c) roi = group[y: y + h, x: x+ w] roi = cv2.resize(roi, (57, 88)) scores = list() for (digit, digitROI) in digits.items(): result = cv2.matchTemplate(cv2.bitwise_not(roi), digitROI, cv2.TM_CCOEFF_NORMED) (_, score, _, _) = cv2.minMaxLoc(result) scores.append(score) ocrNumber = np.argmax(scores) + 1 ocrNumber = ocrNumber if ocrNumber < 10 else 0 groupOutput.append(str(ocrNumber)) # cv2.imshow(f'roi{i}', np.hstack((cv2.bitwise_not(roi), digits[ocrNumber]))) # cv2.imshow(f'roi{i}', np.hstack((cv2.bitwise_not(roi), digits[8]))) # print(ocrNumber) cv2.rectangle(original, (gX - 10, gY - 10), (gX + gW + 10, gY + gH + 10), (0, 0, 255), 2) cv2.putText(original, "".join(groupOutput), (gX, gY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) ocrdNumber = imutils.crop(original, 2900, 3250, 100, 500) # ocrdNumber = cv2.cvtColor(ocrdNumber, cv2.COLOR_BGR2GRAY) output.extend(groupOutput) print("success") return "".join(output), ocrdNumber base = os.path.basename(os.path.normpath(inputImage)) print(f"failed {base}") ocrdNumber = imutils.crop(original, 2900, 3250, 100, 500) # ocrdNumber = cv2.cvtColor(ocrdNumber, cv2.COLOR_BGR2GRAY) return base, ocrdNumber
def crop(self): self._make_dir() self._store_original() ratio = self.image.shape[0] / 1500.0 image = imutils.resize(self.image, 1500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 75, 200) (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5] # loop over the contours for c in cnts: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) # if our approximated contour has four points, then we # can assume that we have found our screen if len(approx) == 4: screenCnt = approx break coords = [] for dot in screenCnt: x = int(round(dot[0][0] * ratio)) y = int(round(dot[0][1] * ratio)) coords.append([[x, y]]) # cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) warped = transform.four_point_transform(image, screenCnt.reshape(4, 2)) # warped = transform.four_point_transform(self.image, screenCnt.reshape(4, 2) * ratio) # self._show(self.image, 'Warped') # warped = transform.four_point_transform(image, screenCnt.reshape(4, 2)) # convert the warped image to grayscale, then threshold it # to give it that 'black and white' paper effect # warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) # warped = threshold_adaptive(warped, 250, offset = 10) # warped = warped.astype("uint8") * 255 # import ipdb; ipdb.set_trace() height, width, x = warped.shape part_0 = imutils.crop(warped, 0, width * 0.24, height * 0.267, height * 0.875) part_1 = imutils.crop(warped, width * 0.24, width * 0.46, height * 0.267, height * 0.875) part_2 = imutils.crop(warped, width * 0.47, width * 0.696, height * 0.267, height * 0.875) part_3 = imutils.crop(warped, width * 0.696, width * 0.968, height * 0.267, height * 0.875) cv2.imwrite(self._new_file('part_0.jpg'), part_0) cv2.imwrite(self._new_file('part_1.jpg'), part_1) cv2.imwrite(self._new_file('part_2.jpg'), part_2) cv2.imwrite(self._new_file('part_3.jpg'), part_3) return self.dir.split('/')[1]
''' Created on Nov 24, 2016 @author: iskandar ''' import numpy as np from argparse import ArgumentParser from imutils import translate, rotate, resize, flip, crop import cv2 ap = ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to image") args = vars(ap.parse_args()) im = cv2.imread("images/dino.jpg") cv2.imshow("Original", translate(im, 50, 100)) cv2.imshow("Rotated", rotate(im, -50)) cv2.imshow("Resized", resize(im, 300.0, 'h')) cv2.imshow("Resized", flip(im, 0)) cv2.imshow("Resized", crop(im, 50, 150, 100, 210)) cv2.waitKey(0)
def OCR(image, box): text_filename = "{}_text.png".format(os.getpid()) cv2.imwrite(text_filename, imutils.crop(image, **box)) text = pytesseract.image_to_string(Image.open(text_filename)) os.remove(text_filename) return text