def onclick(self, event, x, y, flags, param): if len(self.points) == 4: return if event == cv2.EVENT_LBUTTONUP: point = (x, y) self.points.append(point) if len(self.points) == 4: # automatically sort the points points = np.array(self.points, dtype = "float32") self.points = order_points(points)
def get_transformation_matrix(background_image, overlay_image, screen_contour, orientation='right'): overlay_height, overlay_width = overlay_image.shape[:2] screen_coordinates = numpy.float32( order_points([numpy.float32(x[0]) for x in screen_contour]) ) overlay_coordinates = get_overlay_coordinates( overlay_width, overlay_height, orientation, ) return cv2.getPerspectiveTransform( overlay_coordinates, screen_coordinates, )
def order_points(self, pts): return perspective.order_points(pts)
def object_size(filepath, left_width=21): image = cv2.imread(filepath, 0) #gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(image, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # NOTE : Contour - Outlines cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None dimensions = list() for c in cnts: if cv2.contourArea(c) < 100: continue orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") box = perspective.order_points(box) (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) if pixelsPerMetric is None: pixelsPerMetric = dB / left_width dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric if (left_width not in [dimA, dimB]): dimensions.append((dimA, dimB)) max_dim = [-1, -1] for dims in dimensions: if (dims[0] * dims[1] > max_dim[0] * max_dim[1] and 21 not in dims): max_dim[0] = dims[0] max_dim[1] = dims[1] return max_dim
def object_distance(): # Загрузка изображение и поиск контуров image = cv2.imread(args["image"]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) (cnts, _) = contours.sort_contours(cnts) refObj = None # Пробежка по всем объектам for c in cnts: if cv2.contourArea(c) < 100: continue box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") box = perspective.order_points(box) cX = np.average(box[:, 0]) cY = np.average(box[:, 1]) if refObj is None: (tl, tr, br, bl) = box (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) refObj = (box, (cX, cY), D / args["width"]) continue orig = image.copy() cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2) refCoords = np.vstack([refObj[0], refObj[1]]) objCoords = np.vstack([box, (cX, cY)]) for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, COLORS): cv2.circle(orig, (int(xA), int(yA)), 5, color, -1) cv2.circle(orig, (int(xB), int(yB)), 5, color, -1) cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)), color, 2) D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2] (mX, mY) = midpoint((xA, yA), (xB, yB)) cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2) cv2.imshow("Image", orig) cv2.waitKey(0)
(contour, _) = contours.sort_contours(contour) pixelsPerMetric = None for c in contour: if cv2.contourArea(c) < 100: continue orig = image.copy() hull = cv2.convexHull(c) box = cv2.minAreaRect(hull) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") box = perspective.order_points(box) # Reordering the bounding box cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) (tl, tr, br, bl) = box (tophorizx, tophorizy) = midpoint(tl, tr) (bottomhorizx, bottomhorizy) = midpoint(bl, br) (leftvertx, leftverty) = midpoint(tl, bl) (rightvertx, rightverty) = midpoint(tr, br) cv2.circle(orig, (int(tophorizx), int(tophorizy)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(bottomhorizx), int(bottomhorizy)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(leftvertx), int(leftverty)), 5, (255, 0, 0), -1)
def hesap(self): def midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) kamera = cv2.VideoCapture(0) # 0 numaralı kayıtlı kamerayı alma while (True): ret, goruntu = kamera.read() # kamera okumayı başlatma """ # Değişkenlerin yapılandırıldığı ve çözümlendiği kısım ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to the input image") ap.add_argument("-w", "--width", type=float, required=True, help="width of the left-most object in the image (in inches)") args = vars(ap.parse_args()) """ # Görüntü yüklenir, gri tona dönüştürülür ve bulanıklaştırılır. gray = cv2.cvtColor(goruntu, cv2.COLOR_RGB2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) # Kenar tespiti, genişletme ve daraltma işlemleri uygulanır # Nesne kenarları arasındaki boşluklar kapatılır edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # Kenar haritasında kontürler bulunur. cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # Kontürler soldan sağa doğru sıralanır. # "Metrik başına piksel" kalibrasyon değişkeni (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None for c in cnts: # Kontur yeterince büyük değilse görmezden gel if cv2.contourArea(c) < 100: continue # Kontürün döndürülmüş sınırlayıcı kutusunu hesaplanır orig = goruntu.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints( box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # Kontürdeki noktalar görünecek şekilde sıralanır # sol üst, sağ üst, sağ alt ve sol altta # Sınırların ana hatları çizilir box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # Orjinal noktaların üzerinde döngü yapılır ve onlar çizilir for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # sıralı sınırlayıcı kutuyu açın, ardından orta noktayı hesaplar # sol üst ve sağ üst koordinatlar arasında, ardından # sol alt ve sağ alt koordinatlar arasındaki orta nokta (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # sol üst ve sağ üst noktalar arasındaki orta noktayı hesaplar, # ardından sağ üst ve sağ alt arasındaki orta nokta (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # görüntünün orta noktaları çizilir cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # orta noktalar arasında çizgiler çizilir cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # orta noktalar arasındaki Öklid mesafesi hesaplanır dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) print("dA", dA) print("dB", dB) # metrik başına piksel başlatılmadıysa, # bunu piksellerin sağlanan metriğe oranı olarak hesaplanır # (bu durumda, inç) if pixelsPerMetric is None: pixelsPerMetric = 0.0050858 # nesnenin boyutu hesaplanır dimA = dA * pixelsPerMetric * 2.54 dimB = dB * pixelsPerMetric * 2.54 # görüntüdeki nesne boyutları çizilir cv2.putText(orig, "{:.1f}cm".format(dimB), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}cm".format(dimA), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.imshow("Image", orig) # çıktı görüntüsü gösterilir """ plt.imshow(orig) plt.show() """ cv2.waitKey(0) kamera.release() # kamerayı serbest bırak. area_cm = round(dimA * dimB, 1) print("Alan: ", area_cm, "cm^2") """ sad1 = np.size(orig, axis = 0) #(0,1,2) axis=0=24 derinlik sad2 = np.size(orig, axis = 1) #(0,1,2) axis=1=256 satır sad3 = np.size(orig, axis = 2) #(0,1,2) axis=2=256 sütun print(sad1,sad2,sad3) """ print(orig.size) print(orig.shape)
def dim(img, width=1.1): image = cv2.imread(img) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None orig = image.copy() for c in cnts: if cv2.contourArea(c) < 100: continue box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) if pixelsPerMetric is None: pixelsPerMetric = dB / width dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.imwrite(os.path.join(MEDIA_ROOT, "aaa.jpg"), orig)
box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") cv2.drawContours(image, [box], -1, (0, 255, 0), 2) # show the original coordinates #print("Object #{}:".format(i + 1)) #--me-- #print(box) rect = order_points_old(box) if args["new"] > 0: rect = perspective.order_points(box) # show the re-ordered coordinates print(rect.astype("int")) #print("") for ((x, y), color) in zip(rect, colors): cv2.circle(image, (int(x), int(y)), 5, color, -1) #cv2.putText(image, "Object #{}".format(i + 1), #(int(rect[0][0] - 15), int(rect[0][1] - 15)), #cv2.FONT_HERSHEY_SIMPLEX, 0.55, (255, 255, 255), 2) cv2.imwrite("Res/Coor_Res/X_Window_coordinates.png", image)
def object_dimension(directory_obj, pixel_size, lim): """ Calculates the diameters of an object, not circular. It first performs edge detection, then performs a dilation + erosion to close gaps in between object edges. Then for each object in the image, it calcaluates the contourns of the minimum box (minimum rectangle that circumvent the object) and it sorts them from left-to-right (allowing us to extract our reference object). It unpacks the ordered bounding box and computes the midpoint between the top-left and top-right coordinates, followed by the midpoint between bottom-left and bottom-right coordinates. Finally it computes the Euclidean distance between the midpoints. Parameters ---------- directory_obj: str Path of the directory of the image of the object reconstructed at the focal point. pixel_size: float Value of the pixel size (um) lim: int Value of the half shape of the new matrix. It will be the new center Returns ------- orig: :class:`.Image` or :class:`.VectorGrid` Original image plus the dimension of the diameter labelled dimA: float Value of the the first diameter dimB: float Value of the the second diameter ratio: float Value of the ratio of the two diameters """ image = cv2.imread(directory_obj) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #perform the countour gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right (cnts, _) = contours.sort_contours(cnts) for c in cnts: if cv2.contourArea(c) < 5: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear in top-left box = perspective.order_points(box) # Compute the midpoint (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (5, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints # dA variable will contain the height distance (pixels) # dB will hold our width distance (pixels). dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # compute the size of the object dimA = dA * pixel_size dimB = dB * pixel_size diff = dA - dB if diff < 0: ratio = dB / dA else: ratio = dA / dB cv2.putText(orig, "{:.1f}um".format(dimA), (int(tltrX - 15), int(tltrY - 15)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (100, 100, 100), 2) cv2.putText(orig, "{:.1f}um".format(dimB), (int(trbrX + 10), int(trbrY + 15)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (100, 100, 100), 2) return (orig, dimA, dimB, ratio)
image_hsv = cv2.cvtColor(cv2.imread(args["image"]), cv2.COLOR_BGR2HSV) ref_obj_cnt_lst = get_ref_obj_cnts(image_hsv, ref_obj_mask, minArea=100.0, debug=DEBUG) ref_obj_pts_lst = get_ref_obj_pts(ref_obj_cnt_lst) if DEBUG: ref_obj_img = np.zeros(image_hsv.shape, dtype=np.uint8) for pts in ref_obj_pts_lst: cv2.drawContours(ref_obj_img, [pts], -1, (255, 255, 255), 5) cv2.imshow("ref obj", imutils.resize(ref_obj_img, height=600)) if len(ref_obj_pts_lst) == 1: # There should be only one reference object match in the image in order to calculate the refernce dimensions accurately (tl, tr, br, bl) = perspective.order_points(ref_obj_pts_lst[0]) (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and bottom-left points, # followed by the midpoint between the top-right and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) px_width = max(dA, dB) if pixelsPerMetric is None:
def ibm(time): # load the image image = cv2.imread("cita.jpg") # define the list of boundaries boundaries = ([0, 80, 0], [100, 255, 100]) lower = np.array(boundaries[0], dtype="uint8") upper = np.array(boundaries[1], dtype="uint8") # find the colors within the specified boundaries and apply # the mask mask = cv2.inRange(image, lower, upper) output = cv2.bitwise_and(image, image, mask=mask) naming = "image.jpg" # show the images cv2.imwrite(naming, output) def midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5) num = 0 width = 1 # load the image, convert it to grayscale, and blur it slightly #image2 = cv2.imread("image.jpg") #image2 = cv2.imread("pokemon_games.png") image2 = image gray = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) cv2.imwrite("grayscale.jpg", gray) gray = cv2.GaussianBlur(gray, (7, 7), 0) cv2.imwrite("blur.jpg", gray) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) cv2.imwrite("canny.jpg", edged) edged = cv2.dilate(edged, None, iterations=1) cv2.imwrite("dilate.jpg", edged) edged = cv2.erode(edged, None, iterations=1) cv2.imwrite("erode.jpg", edged) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #print(cnts) cnts = imutils.grab_contours(cnts) #print(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None #cuman buat gambar ajah # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 300: continue # compute the rotated bounding box of the contour orig = image.copy() #orig = cv2.imread("graphicdesignlove.jpg") box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) tlbl = (tlblX, tlblY) trbr = (trbrX, trbrY) (cx, cy) = midpoint(tlbl, trbr) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) cv2.putText(orig, time, (0, orig.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: pixelsPerMetric = dB / width # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric # draw the object sizes on the image cv2.putText(orig, "H = {:.1f}cm".format(dimA), (int(cx) + 10, int(cy) - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "W = {:.1f}cm".format(dimB), (int(cx) + 10, int(cy) + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image filename = "/var/www/html/object" + str(num) + ".jpg" num = num + 1 print(filename) cv2.imwrite(filename, orig)
result = cv2.merge((b1, g1, r1)) return cv2.cvtColor(result, cv2.COLOR_YCrCb2BGR) if __name__ == '__main__': filename = sys.argv[1] img = cv2.imread(filename) squares = find_squares(img) print >> sys.stderr, 'n squares', len(squares) histogram_equalize(img) drawn = img.copy() cv2.drawContours(drawn, squares, -1, (0, 255, 0), 5 ) show('squares', drawn) postit_size = 200 pts2 = np.float32([[0,0],[0,postit_size],[postit_size,postit_size],[postit_size,0]]) for number, square in enumerate(squares, 1): print square, pts2 M = cv2.getPerspectiveTransform( perspective.order_points(square.astype('float32')), perspective.order_points(pts2), ) dst = cv2.warpPerspective(img, M, (postit_size, postit_size)) histogram_equalize(dst) # show('one', dst) outname = 'yomama-postit-%02i.png' % number cv2.imwrite(outname, dst)
def camera_callback(self, data): # Return if node is not enabled if (not self.is_node_enabled): return # Node is enabled, process the camera data try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) self.tool_ROI = RegionOfInterest() self.tool_pos = ObjectPose() self.tool_pos.header.stamp.secs = self.camera_secs self.tool_pos.header.stamp.nsecs = self.camera_nsecs WW=self.image_width HH=self.image_height fx=self.camera_K[0] fy=self.camera_K[4] u0=self.camera_K[5] v0=self.camera_K[2] K=np.matrix([[fx, 0, u0, 0], [0, fy, v0, 0], [0, 0, 1, 0]]) K_INV=pinv(K) img = cv_image.copy() output = img.copy() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (11, 11), 0) #thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)[1] thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,11,2) # perform edge detection, then perform a dilation + erosion to close gaps in between object edges edged = cv2.Canny(blurred, 20, 150) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) edged2 = auto_canny(blurred) edged3 = cv2.dilate(edged2.copy(), None, iterations=1) edged4 = cv2.erode(edged3.copy(), None, iterations=1) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(12,24)) filled = cv2.morphologyEx(edged4, cv2.MORPH_CLOSE, kernel) # find contours in the thresholded image and initialize the shape detector #cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) #cnts = cv2.findContours(edged4.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cv2.findContours(filled.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the (cnts, _) = contours.sort_contours(cnts) # loop over the contours simil = [] cX_v= [] cY_v= [] wr_cent_v= [] wr_contours = [] t_h_v = [] wr_tc_v = [] wrenches = [] wr_count = 0 toolIdentified = False for c in cnts: # compute the center of the contour, then detect the name of the # shape using only the contour M = cv2.moments(c) hu = cv2.HuMoments(M) retSim1 = cv2.matchShapes(cnts_s[0],c,1,0.0) retSim2 = cv2.matchShapes(cnts_s[0],c,2,0.0) retSim3 = cv2.matchShapes(cnts_s[0],c,3,0.0) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape on the image c = c.astype("float") c *= 1 c = c.astype("int") text = "{}".format(shape) area = cv2.contourArea(c) # approximate the contour #peri = cv2.arcLength(c, True) #approx = cv2.approxPolyDP(c, 0.01 * peri, True) (x, y, w, h) = cv2.boundingRect(c) #print(img.shape[0]) # if the contour is too large or too small, ignore it if h < 0.3*img.shape[0] or x<5 or y<5 or x+w > WW-5 or y+h > HH-5: continue aspectRatio = w / float(h) (xc,yc),radius = cv2.minEnclosingCircle(c) minEncCirArea = math.pi*(radius**2) minEncircleA_ratio = minEncCirArea/(area+1) # compute the rotated bounding box of the contour box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) (tl, tr, br, bl) = box #out3=img.copy() #print(aspectRatio,retSim1,retSim2,retSim3) #cv2.drawContours(out3, [c], -1, (0, 0, 255), 3) #cv2.imshow("out3", out3) #cv2.waitKey(3) dA = distance.euclidean(tl, bl) dB = distance.euclidean(tl, tr) aspectRatio2 = dB/dA minRectArea_ratio = (dA*dB)/(area+1) hull = cv2.convexHull(c) hull_area = cv2.contourArea(hull) solidity = float(area)/(hull_area+1) keepRatio = aspectRatio > 0.9 and aspectRatio < 1.15 #keepSimilarity = retSim1 < 2.9 and retSim2 < 2 and retSim3 < 1.5 # tune for outdoor specially retSim2 keepSimilarity = retSim2 < 2 and retSim3 < 1.0 #keepSolidity = solidity > 0.4 and solidity < 0.7 #keepAreaRatio = minRectArea_ratio > 2 and minRectArea_ratio < 3 #Circle = len(approx)>8 and aspectRatio > 0.8 and aspectRatio < 1.3 and minEncircleA_ratio > 1 and minEncircleA_ratio < 1.3 if keepRatio and keepSimilarity: wr_count = wr_count + 1 #cX = int((M["m10"] / M["m00"])) #cY = int((M["m01"] / M["m00"])) #cX_v = np.hstack((cX_v,cX)) #cY_v = np.hstack((cY_v,cY)) #wr_cent = (cX,cY) wrs_contour = c cv2.rectangle(output, (x,y), (x+w,y+h), (255,0,0), 2) cv2.drawContours(output, [box.astype("int")], -1, (0, 0, 255), 2) cv2.drawContours(output, [c], -1, (0, 255, 0), 2) subimg = img[y+h/2:y+h,x:x+w] subfilled = filled[y+h/2:y+h,x:x+w] subout = subimg.copy() #cv2.imshow("subimg", subimg) #cv2.waitKey(3) subcnts = cv2.findContours(subfilled.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) subcnts = subcnts[0] if imutils.is_cv2() else subcnts[1] detected_sizes = [] wr_subcnts = () if len(subcnts) > 0: # sort the contours from left-to-right and initialize the (subcnts, _) = contours.sort_contours(subcnts) subcnt_idx = 0 for subc in subcnts: cv2.drawContours(subout, [subc], -1, (0, 255, 0), 2) (x_sub, y_sub, w_sub, h_sub) = cv2.boundingRect(subc) # compute the rotated bounding box of the contour sbox = cv2.minAreaRect(subc) sbox = cv2.cv.BoxPoints(sbox) if imutils.is_cv2() else cv2.boxPoints(sbox) sbox = np.array(sbox, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box sbox = perspective.order_points(sbox) (tl_sub, tr_sub, br_sub, bl_sub) = sbox dH_sub = distance.euclidean(tl_sub, bl_sub) dW_sub = distance.euclidean(tl_sub, tr_sub) #if dH_sub*dW_sub*1.0 < 0.02*h*w: if h_sub < 0.2*h: continue subcnt_idx = subcnt_idx + 1; wrs_Z = fx*(act_wrs_w/w) wr_h=wrs_Z*dH_sub/fx wr_w=wrs_Z*dW_sub/fx #wr_h=wrs_Z*h_sub/fx #wr_w=wrs_Z*w_sub/fx h_offset = wrs_Z*(h/2)/fx - 0.01 tool_wm = wr_w tool_hm= wr_h + h_offset #pnl_cX = pnl_x + pnl_w/2 #pnl_cY = pnl_y + pnl_h/2 #p_pxl_hom=np.matrix([[pnl_cY],[pnl_cX],[1]]) #P_mtr_hom=np.dot(K_INV,p_pxl_hom) #P_mtr=P_mtr_hom*(pnl_Z/P_mtr_hom[2][0]) cv2.drawContours(subout, [sbox.astype("int")], -1, (0, 0, 255), 2) #cv2.rectangle(output, (x_sub+x,y_sub+y), (x_sub+x+w_sub,y_sub+y+h_sub+h/2), (255,0,0), 2) #print(tl_sub) #print('Hello') cv2.putText(subout, "W={:.2f}".format(wr_w*1000), (x_sub,y_sub+h_sub/2-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.putText(subout, "H={:.2f}".format((wr_h + h_offset)*1000), (x_sub,y_sub+h_sub/2+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) #cv2.circle(output, (pnl_cX, pnl_cY), 3, (0, 0, 255), -1) size = self.label(np.array([tool_wm*1000,tool_hm*1000])) #size = self.label(np.array([tool_wm*1000])) detected_sizes=np.append(detected_sizes,size) #print(type(wr_subcnts),type(subc)) wr_subcnts = wr_subcnts + (subc,) if size == self.tool_size: #tool_contour = wr toolIdentified = True tool_indx = subcnt_idx #tool_x = x_sub+x #tool_y = y_sub+y #tool_w = w_sub #tool_h = h_sub+h/2 #print(tool_x,tool_y,tool_w,tool_h) #print(x,y,w,h,x_sub,y_sub,w_sub,h_sub) cv2.imshow("subout", subout) cv2.waitKey(3) #print(detected_sizes) if len(detected_sizes) > 4 and len(detected_sizes) < 7 : if toolIdentified: self.tool_indx_vec = np.append(self.tool_indx_vec,tool_indx) # is np.append memory an issue? self.tool_indx_vec = self.tool_indx_vec[-2*self.win_size:] tool_indx_win = self.tool_indx_vec[-self.win_size:] hist, bin_edges = np.histogram(tool_indx_win,array(range(1,len(detected_sizes)+1)), density=True) self.right_tool_idx = np.argmax(hist) self.confidence = hist[self.right_tool_idx] #print(array(range(1,len(detected_sizes)+1))) if self.right_tool_idx > 0 and len(self.tool_indx_vec) > self.win_size: (x_sub, y_sub, w_sub, h_sub) = cv2.boundingRect(wr_subcnts[self.right_tool_idx]) tool_x = x_sub+x tool_y = y_sub+y tool_w = w_sub tool_h = h_sub+h/2 #subout2 = subimg.copy() #cv2.rectangle(subout2, (int(x_sub), int(y_sub)), (int(x_sub) + int(w_sub), int(y_sub) + int(h_sub)), (255, 0, 255), 2) #cv2.imshow("subout2", subout2) #cv2.waitKey(10) cv2.rectangle(output, (int(tool_x), int(tool_y)), (int(tool_x) + int(tool_w), int(tool_y) + int(tool_h)), (255, 0, 255), 2) self.tool_ROI.x_offset = tool_x self.tool_ROI.y_offset = tool_y self.tool_ROI.width = tool_w self.tool_ROI.height = tool_h #self.tool_ROI_pub.publish(self.tool_ROI) tool_cX = tool_x + tool_w/2 tool_cY = tool_y + tool_h/2 tool_pxl_hom=np.matrix([[tool_cY],[tool_cX],[1]]) tool_mtr_hom=np.dot(K_INV,tool_pxl_hom) tool_mtr=tool_mtr_hom*(wrs_Z/tool_mtr_hom[2][0]) cv2.putText(output, "X={}".format(-tool_mtr[0][0]), (30, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.putText(output, "Y={}".format(-tool_mtr[1][0]), (30, 90), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.putText(output, "Z={}".format(tool_mtr[2][0]), (30, 120), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.putText(output, "Confidence={:.2f}".format(self.confidence), (30, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.85, (255, 0, 255), 2) cv2.circle(output, (int(tool_cX), int(tool_cY)), 3, (0, 0, 255), -1) self.tool_pos.pose.position.x = -tool_mtr[0][0] self.tool_pos.pose.position.y = -tool_mtr[1][0] self.tool_pos.pose.position.z = tool_mtr[2][0] #self.tool_pos.header.stamp.secs = int(str(self.camera_secs)[-3:]) self.tool_pos.header.stamp.secs = self.camera_secs self.tool_pos.header.stamp.nsecs = self.camera_nsecs self.tool_pos.confidence = self.confidence #self.tool_pos_pub.publish(self.tool_pos) self.tool_pos_pub.publish(self.tool_pos) self.tool_ROI_pub.publish(self.tool_ROI) # show the output image cv2.imshow("out2", output) cv2.waitKey(3)
sudoku = SolveSudoku(grid) grid = sudoku.solve() #fill the solved numbers in empty cells for row, col in gz_indices: cv2.putText(warped, str(grid[row][col]), tuple(gz_centers[row][col]), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 2) cv2.imshow("Solved", warped) cv2.waitKey(0) #process the src and dst points pt_src = [[0, 0], [warped.shape[1], 0], [warped.shape[1], warped.shape[0]], [0, warped.shape[0]]] pt_src = np.array(pt_src, dtype="float") pt_dst = poly.reshape(4, 2) pt_dst = pt_dst.astype("float") #align points in order pt_src = order_points(pt_src) pt_dst = order_points(pt_dst) #calculate homography matrix H, _ = cv2.findHomography(pt_src, pt_dst) #reproject the puzzle to original image im_out = cv2.warpPerspective(warped, H, dsize=(gray.shape[1], gray.shape[0])) im_out = cv2.addWeighted(gray, 0.9, im_out, 0.2, 0) cv2.imshow("Projected", im_out) cv2.waitKey(0)
def object_size(image, cnts, width): # width of quarter (in inches) #width = 3.5 # load the image, covert it to grayscale, blur it slightly #image = cv2.imread('slide.jpg') # gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # gray = cv2.medianBlur(gray,7) # invert the image color # inv_image = cv2.bitwise_not(gray) # perform edge detection, then perform a dilation + erosion to close gaps in b/w object edges # edged = cv2.Canny(inv_image, 50, 100) # edged = cv2.dilate(edged, None, iterations=1) # edged = cv2.erode(edged, None, iterations=1) # Calculate the threshold for the edges of the object in the image # _, threshold = cv2.threshold(inv_image, 20, 255, 0) # find contours in the edge map # cnts = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue #compute the rotated bounding box of the contour box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype='int') # order the points in the contour such that they appear in # top-left, top-right, bottom-right, bottom-left order # then draw the outline of the rotated bounding box box = perspective.order_points(box) #cv2.drawContours(image, [box.astype("int")], -1, (0,255,0), 2) # loop over the original points and draw them #for (x,y) in box: # cv2.circle(image, (int(x), int(y)), 5, (0,0,255), -1) # unpack the ordered bounding box, then compute the midpoint b/w the top-left and top-right coordinates # then compute the midpoint b/w the bottom left and bottom right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint b/w the top-left and bottom-left points # then compute the midpoint b/w the top-right and bottom right points (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image #cv2.circle(image, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) #cv2.circle(image, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) #cv2.circle(image, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) #cv2.circle(image, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines b/w the midpoints #cv2.line(image, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) #cv2.line(image, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255,0,255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then compute it as the ratio of pixels to supplied metric # in this case, mm if pixelsPerMetric is None: pixelsPerMetric = dB / width # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric # draw the object sizes on the image #cv2.putText(image, "{:.1f}mm".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0), 2) #cv2.putText(image, "{:.1f}mm".format(dimB), (int(trbrX - 15), int(trbrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0), 2) # show the output image #cv2.imshow("Image", orig) #cv2.waitKey(0) return pixelsPerMetric
def imagedimenstion(self,images): assume_width=2 dims_width=[] dims_length=[] print(images) imageA = cv2.imread(images[0]) imageA = cv2.resize(imageA, (0, 0), None, .5, .5) # just resized the image to a half of its original size imageB = cv2.imread(images[1]) imageB = cv2.resize(imageB, (0, 0), None, .5, .5) #concat both images to form one image with original at the left numpy_horizontal_concat = np.concatenate((imageA, imageB), axis=1) #output file created #now finding the dimenstion image = numpy_horizontal_concat gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = self.midpoint(tl, tr) (blbrX, blbrY) = self.midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = self.midpoint(tl, bl) (trbrX, trbrY) = self.midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),(255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),(255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: # pixelsPerMetric = dB / args["width"] pixelsPerMetric = dB / assume_width # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric # draw the object sizes on the image cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (0, 0, 0), 2) cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,0.65, (0, 0, 0), 2) dims_width.append(dimB) dims_length.append(dimA) # show the output image # cv2.imshow("Image", orig) # cv2.waitKey(0) print("Widths",dims_width) inc_width=((dims_width[1]-dims_width[0])/dims_width[0])*100 print("width inc percent",round(inc_width , 2) ,"%") print("Height",dims_length) dec_length=((dims_length[0]-dims_length[1])/dims_length[0])*100 print("Height dec percent",round(dec_length , 2) ,"%") return inc_width
cv2.createTrackbar('k-size', 'image', 1, 19, nothing) cv2.createTrackbar('o-size', 'image', 1, 19, nothing) cv2.createTrackbar('c-size', 'image', 1, 19, nothing) # cv2.createTrackbar('h','image',,255,nothing) # # Set default value for MAX HSV trackbars. cv2.setTrackbarPos('HMin', 'image', 0) cv2.setTrackbarPos('HMax', 'image', 179) cv2.setTrackbarPos('SMax', 'image', 255) cv2.setTrackbarPos('VMax', 'image', 255) width = 629 height = 503 rect = perspective.order_points( np.array([(162, 34), (1123, 35), (20, 819), (1247, 824)])) # r = np.array([(8.0, 906.0), (109.0, 22.0), (1163, 36), (1255, 899)]) # print(r) # h = home() (tl, tr, br, bl) = rect widthA = np.sqrt(((br[0] - bl[0])**2) + ((br[1] - bl[1])**2)) widthB = np.sqrt(((tr[0] - tl[0])**2) + ((tr[1] - tl[1])**2)) maxWidth = max(int(widthA), int(widthB)) # compute the height of the new image, which will be the # maximum distance between the top-right and bottom-right # y-coordinates or the top-left and bottom-left y-coordinates heightA = np.sqrt(((tr[0] - br[0])**2) + ((tr[1] - br[1])**2)) heightB = np.sqrt(((tl[0] - bl[0])**2) + ((tl[1] - bl[1])**2)) maxHeight = max(int(heightA), int(heightB)) # now that we have the dimensions of the new image, construct
def process(self, width): sizes = [] for c in self.contours: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = self.image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints( box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image # cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) # cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) # cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) # cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints # cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), # (255, 0, 255), 2) # cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), # (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if self.pixelsPerMetric is None: self.pixelsPerMetric = dB / width # compute the size of the object dimA = dA / self.pixelsPerMetric dimB = dB / self.pixelsPerMetric sizes.append((dimA, dimB)) # draw the object sizes on the image # cv2.putText(orig, "{:.1f}in".format(dimA), # (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, # 0.65, (255, 255, 255), 2) # cv2.putText(orig, "{:.1f}in".format(dimB), # (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, # 0.65, (255, 255, 255), 2) # show the output image # cv2.imshow("Image", orig) # cv2.waitKey(0) self.sizes = sizes
def main(): init() while True: # Read frame _, image = cam.read() # Mirror if mirror: image = cv2.flip(image, flipCode=1) # Grayscale gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Blur blur = 1 + 2 * cv2.getTrackbarPos('blur', 'BARS') blurred = cv2.medianBlur(gray, blur) # Canny and morphs sigma = cv2.getTrackbarPos('sigma', 'BARS') / 100 v = np.median(blurred) canny_low = int(max(0, (1 - sigma) * v)) canny_high = int(min(255, (1 + sigma) * v)) # canny_low = cv2.getTrackbarPos('canny_low', 'BARS') # canny_high = 3 * canny_low # canny_high = max(canny_low, cv2.getTrackbarPos('canny_high', 'BARS')) edged = cv2.Canny(blurred, canny_low, canny_high) edged = cv2.dilate(edged, None, iterations=3) edged = cv2.erode(edged, None, iterations=2) # Find contours _, contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) x0, y0 = image.shape[:2] diag0 = math.hypot(x0, y0) # l = 400 l = max(0, cv2.getTrackbarPos('l', 'BARS')) * 10 hfov = 54.5 vfov = 42.3 # dfov = 66.17 dfov = cv2.getTrackbarPos('dfov', 'BARS') width_mm, height_mm, diag_mm = getWHImage(l, hfov, vfov, dfov) ratio = diag_mm / diag0 for contour in contours: if cv2.contourArea(contour) < 200: continue # @rect :: tuple((center_x, center_y), (width, height), angle_deg) # box :: np.array([[x y], [x y], [x y], [x y]]) -- ordered as (top_left, top_right, bottom_right, bottom_left) # dA -- width [px] # dB -- height [px] # dD -- diagonal [px] # dimA, dimB, dimD -- same as above [mm] rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.array(box, dtype="int") box = perspective.order_points(box) (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) dA = math.hypot(tltrX - blbrX, tltrY - blbrY) dB = math.hypot(tlblX - trbrX, tlblY - trbrY) dD = math.hypot(dA, dB) xoc, yoc = midpoint(tl, br) xcc = x0 - yoc ycc = y0 - xoc alpha = math.atan2(dB, dA) dimD = dD * ratio dimA = dimD * math.sin(alpha) # Why not cos? dimB = dimD * math.cos(alpha) for x, y in box: cv2.circle(image, (x, y), 5, (255, 0, 255), 2) cv2.drawContours(image, [box.astype('int')], -1, (0, 255, 0), 2) cv2.drawContours(image, [contour], -1, (0, 0, 255), 2) put_text(image, (tltrX - 15, tltrY - 10), dimA) # width put_text(image, (trbrX + 10, trbrY), dimB) # height edged_bgr = cv2.cvtColor(edged, cv2.COLOR_GRAY2BGR) cv2.imshow('MAIN', np.hstack([image, edged_bgr])) if cv2.waitKey(1) == 27: break cv2.destroyAllWindows() cam.release()
def pill_cv(): '''perform computer vision and return (shape: str, colour: tuple, size: sorted list, image: str of img_path, error: T/F)''' error = False timenow = time.time() ### CAPTURE IMAGE camera = PiCamera(resolution='2592x1700') camera.brightness = 60 camera.awb_mode = 'off' camera.awb_gains = (2, 1.5) #(red gain, blue gain) camera.start_preview() sleep(2) predicted_image = '/home/pi/Desktop/Capstone_rpi_integrate/local_image/{}orig.jpg'.format( timenow) camera.capture(predicted_image) camera.stop_preview() camera.close() ### PREDICT SHAPE img = cv2.imread(predicted_image) new_img = cv2.resize( img, (320, 320)) #resize dimension obtained from tensor input details interpreter.set_tensor(input_details[0]['index'], [new_img]) interpreter.invoke() rects = interpreter.get_tensor(output_details[0]['index']) label = interpreter.get_tensor(output_details[1]['index']).tolist() scores = interpreter.get_tensor(output_details[2]['index']) score_highest = np.max(scores[0]) #get highest score score_highest_index = np.argwhere(scores[0] == score_highest) #get second highest score score_copy = scores[0].copy() unique, counts = np.unique(score_copy, return_counts=True) counts_score = dict(zip(unique, counts)) times = counts_score[score_highest] for i in range(times): score_copy = np.delete(score_copy, score_highest) score_2highest = np.max(score_copy[0]) print('score 2nd: ', score_2highest) # retrieve prediction of highest confidence score for index, score in enumerate(scores[0]): if score == score_highest: # if score > 0.8: predicted_shape = label_names[ int(label[0][index]) + 1] #need to +1 cos the first label is background box = rects[0][index] print('Predicted label: {}, Score: {}'.format( predicted_shape, score)) # break if score == score_2highest: box2 = rects[0][index] # check IoU iou = IoU(box, box2) print('iou: ', iou) if iou > 0.5: error = True # draw bounding box and prediction in the img_shape img_shape = img.copy() draw_rect(img_shape, box) cv2.putText(img_shape, "{} {}".format(predicted_shape, score), (200, 100), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3) ### GET MASK # get cropped image height, width, _ = img.shape y_min = int(max(1, (box[0] * height))) x_min = int(max(1, (box[1] * width))) y_max = int(min(height, (box[2] * height))) x_max = int(min(width, (box[3] * width))) # crop_img = img[y_min:y_max, x_min:x_max] # get slightly larger cropped image cropxl_img = img[max(y_min - 30, 0):min(y_max + 30, height), max(x_min - 30, 0):min(x_max + 30, width)] cropxl_hsv = cv2.cvtColor(cropxl_img.copy(), cv2.COLOR_BGR2HSV) # extract saturation value and get pill mask _, s, _ = cv2.split(cropxl_hsv) _, thresh_s = cv2.threshold(s, 10, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) s_filtered = cv2.GaussianBlur(thresh_s, (5, 5), 11) # smoothen the edges ### GET COLOUR s_maskrgb = cv2.bitwise_and(cropxl_img.copy(), cropxl_img.copy(), mask=s_filtered) rgb_colours = get_colours_kmeans(timenow, s_maskrgb, colour_count=2, show_chart=True) # remove black predicted_colour = [] for rgb in rgb_colours: rgb = rgb.tolist() if rgb != [0, 0, 0]: predicted_colour = predicted_colour + rgb print('Predicted colour: ', predicted_colour) # set threshold for the contour area max_thresh_contour = cropxl_img.shape[0] * cropxl_img.shape[1] * 0.99 min_thresh_contour = cropxl_img.shape[0] * cropxl_img.shape[1] * 0.2 img_s = cropxl_img.copy() # find contours contours_s, _ = cv2.findContours(s_filtered, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contour_list_s = [] area_list_s = [] for contour in contours_s: area = cv2.contourArea(contour) if area > min_thresh_contour and area < max_thresh_contour: contour_list_s.append(contour) area_list_s.append(area) try: # draw box of the largest contour index_max = area_list_s.index(max(area_list_s)) contour = contour_list_s[index_max] box = cv2.minAreaRect(contour) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") cv2.drawContours(img_s, [box.astype("int")], -1, (255, 0, 0), 2) # print(box) ### GET SIZE box = perspective.order_points(box) (tl, tr, br, bl) = box # print(box) (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # compute object size # pixelsPermm = 540/10.6 # pixelsPermm = 50.9 # value obtained from experiment pixelsPermm = 48.71 dimA = round(dA / pixelsPermm, 1) dimB = round(dB / pixelsPermm, 1) predicted_size = [dimA, dimB] predicted_size.sort() print("Predited size in mm: {}".format(predicted_size)) # insert the object sizes on the image cv2.putText(img_s, "{:.1f}mm".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) cv2.putText(img_s, "{:.1f}mm".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2) cv2.drawContours(img_s, contour_list_s, -1, (20, 200, 0), 2) #draw contours except: error = True # img_shape_resize = cv2.resize(img_shape.copy(), (int(width*0.5), int(height*0.5)), interpolation = cv2.INTER_AREA) # display and save images # cv2.imshow('Original', img) # cv2.imshow('Predicted shape', img_shape) # sleep(3) # cv2.imshow('Saturation',s) # cv2.imshow('Threshold-saturation with smoothing', s_filtered) # sleep(2) # cv2.imshow('Pill mask', s_maskrgb) # sleep(2) # cv2.imshow('Predicted size', img_s) cv2.imwrite( "/home/pi/Desktop/Capstone_rpi_integrate/local_image/{}shape.jpg". format(timenow), img_shape) cv2.imwrite( "/home/pi/Desktop/Capstone_rpi_integrate/local_image/{}maskbw.jpg". format(timenow), s_filtered) cv2.imwrite( "/home/pi/Desktop/Capstone_rpi_integrate/local_image/{}maskrgb.jpg". format(timenow), s_maskrgb) cv2.imwrite( "/home/pi/Desktop/Capstone_rpi_integrate/local_image/{}size.jpg". format(timenow), img_s) # cv2.waitKey(300) # cv2.destroyAllWindows() # dummy: to delete # predicted_shape = 'round' # predicted_colour = [128, 128, 128] # predicted_size = [10, 14] # predicted_image = '/home/pi/Desktop/gcloud_test.jpg' # error = False return predicted_shape, predicted_colour, predicted_size, predicted_image, error
erode = cv2.erode(edged, kernal, iterations=1) counts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) counts = imutils.grab_contours(counts) print("Total number of contours are: ", len(counts)) (counts, _) = contours.sort_contours(counts) counts = [x for x in counts if cv2.contourArea(x) > 100] objects = counts[0] border = cv2.minAreaRect(objects) border = cv2.boxPoints(border) border = np.array(border, dtype="int") border = perspective.order_points(border) (tl, tr, br, bl) = border dist_in_pixel = euclidean(tl, tr) dist_in_cm = 2 pixel_per_cm = dist_in_pixel / dist_in_cm for count in counts: border = cv2.minAreaRect(count) border = cv2.boxPoints(border) border = np.array(border, dtype="int") border = perspective.order_points(border) (tl, tr, br, bl) = border cv2.drawContours(morh, [border.astype("int")], -1, (0, 0, 255), 2) mid_pt_horizontal = (tl[0] + int(abs(tr[0] - tl[0]) / 2), tl[1] + int(abs(tr[1] - tl[1]) / 2)) mid_pt_verticle = (tr[0] + int(abs(tr[0] - br[0]) / 2),
def measure_length(img, width): i = 0 gray = cv2.GaussianBlur(img, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.erode(cv2.dilate(cv2.Canny(gray, 50, 100), None, iterations=1), None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixels_per_metric = None # loop over the contours individually for countour in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(countour) < 100: continue # compute the rotated bounding box of the contour orig = img.copy() box = cv2.minAreaRect(countour) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) img = cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) d_b = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixels_per_metric is None: pixels_per_metric = d_b / width # compute the size of the object dim_a = (dA / pixels_per_metric) dim_b = d_b / pixels_per_metric # draw the object sizes on the image cv2.putText(orig, "{:.1f}cm".format(dim_a), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}cm".format(dim_b), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) i += 1 cv2.imwrite("object_length_{}.png".format(i), orig)
def getShadowSize(thresh, x, y, roi_x, roi_y, img, neighbors): # определяем минимальную дистанцию от здания до пикселей тени min_dist = thresh.shape[0] min_dist_coords = (0, 0) # 0 0 y = int(thresh.shape[0] / 2) x = int(thresh.shape[1] / 2) for i in range(thresh.shape[0]): for j in range(thresh.shape[1]): if (thresh[i, j] == 255) and (math.sqrt((i - y) * (i - y) + (j - x) * (j - x)) < min_dist): min_dist = math.sqrt((i - y) * (i - y) + (j - x) * (j - x)) min_dist_coords = (i, j) # y, x print('min_dist = %s' % min_dist) if min_dist <= thresh.shape[0] / 4: # определяем сегмент, который содержит пиксель с минимальным расстоянием до здания import queue as queue q_coords = queue.Queue() q_coords.put(min_dist_coords) mask = thresh.copy() # cv2.imshow('shadow0', mask) # cv2.waitKey(0) output_coords = list() output_coords.append(min_dist_coords) while q_coords.empty() == False: currentCenter = q_coords.get() for idx1 in range(3): for idx2 in range(3): offset1 = -1 + idx1 offset2 = -1 + idx2 currentPoint = [ currentCenter[0] + offset1, currentCenter[1] + offset2 ] if (currentPoint[0] >= 0 and currentPoint[0] < mask.shape[0]): if (currentPoint[1] >= 0 and currentPoint[1] < mask.shape[1]): if (mask[currentPoint[0]][currentPoint[1]] == 255): mask[currentPoint[0]][currentPoint[1]] = 100 img[currentPoint[0] + roi_y][currentPoint[1] + roi_x] = [0, 0, 255] q_coords.put(currentPoint) output_coords.append(currentPoint) # all changed 0 to 1 # отрисовываем ближайшую тень mask = np.zeros_like(mask) for i in range(len(output_coords)): mask[output_coords[i][0]][output_coords[i][1]] = 255 # img[currentPoint[0] + roi_y][currentPoint[1] + roi_x] = [0, 0, 255] # cv2.imshow('red shadow', img) cv2.waitKey(0) img1 = mask.copy() # cv2.imshow('shad', mask) # cv2.waitKey(0) # find contours in the edge map cnts = cv2.findContours(img1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[1] #if imutils.is_cv2() else cnts[1] img2 = cv2.cvtColor(img1, cv2.COLOR_GRAY2BGR) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # print(len(cnts), '-----------') # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it # if cv2.contourArea(c) < 10000: # continue # compute the rotated bounding box of the contour # orig = thresh.copy() box = cv2.minAreaRect(c) box = cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(img2, [box.astype("int")], -1, (0, 255, 0), 1) # loop over the original points and draw them for (x, y) in box: # cv2.circle(img2, (int(y), int(x)), 2, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(img2, (int(tltrX), int(tltrY)), 1, (255, 0, 0), -1) cv2.circle(img2, (int(blbrX), int(blbrY)), 1, (255, 0, 0), -1) # cv2.circle(img2, (int(tlblX), int(tlblY)), 1, (255, 0, 0), -1) # cv2.circle(img2, (int(trbrX), int(trbrY)), 1, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(img2, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 1) # cv2.line(img2, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), # (255, 0, 255), 1) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) if dA == 0: dA = 1 # dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # draw the object sizes on the image # cv2.putText(img2, "{:.1f}in".format(dA), # (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, # 0.15, (255, 255, 255), 2) # cv2.putText(img2, "{:.1f}in".format(dB), # (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, # 0.15, (255, 255, 255), 2) print('dA = %s' % (str(dA))) print('dB = %s' % (str(dB))) scale_img2 = img2.copy() scale_img2 = cv2.resize( scale_img2, (int(scale_img2.shape[1] * 5), int(scale_img2.shape[0] * 5))) cv2.imshow('img points', scale_img2) # cv2.waitKey(0) cv2.imwrite(dir + "img2.png", scale_img2) dA = int(dA) # определяем размер(длину) тени при помощи морфологической операции erode kernel = np.ones((3, 3), np.uint8) i = 0 while np.count_nonzero(mask) != 0: mask = cv2.erode(mask, kernel, iterations=1) i += 1 # cv2.imshow('shadow erode = ' + str(i), mask) # cv2.waitKey(0) print('i = %s' % (str(i))) # correct result for small objects if i == 1: dA = max(dA, dB) if dA == 0 and dB == 0: dA = 1 # correct result for shadows with neighbours if neighbors > 0 and dA / i > 2: dA = i + 1 else: dA = 1 return dA
def mainfunction(startandend=None): # there should be 50 images? 10 length cuts and 5 breadth cuts topLeft = xml_coordinates[0] height = abs(xml_coordinates[0][0]-xml_coordinates[3][0]) width = abs(xml_coordinates[0][1]-xml_coordinates[1][1]) transformed_boxes = [] #TODO: CHANGE THIS for index, image_slice in enumerate(os.listdir(path)): # load the image, convert it to grayscale, and blur it slightly # Also defines pixel coordinate limits of image 900 for x and 350 for y # image = cv2.resize(cv2.imread(image_slice), (IMAGE_WIDTH, IMAGE_HEIGHT)) image = cv2.imread(path+"\\"+image_slice) IMAGE_HEIGHT, IMAGE_WIDTH = image.shape[0:2] im_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) # create NumPy arrays from the boundaries lower = np.array(LB_ARG, dtype = "uint8") upper = np.array(UB_ARG, dtype = "uint8") # find the colors within the specified boundaries and apply the mask mask = cv2.inRange(im_gray, lower, upper) im_mask = cv2.bitwise_and(im_gray, im_gray, mask = mask) thresh = 0; im_fill = cv2.threshold(im_mask, thresh, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]; _,contour,hier = cv2.findContours(im_fill,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) for cnt in contour: cv2.drawContours(im_fill,[cnt],0,255,-1) gray = cv2.GaussianBlur(im_fill, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] if len(cnts) == 0: continue (cnts, _) = contours.sort_contours(cnts) width_ = width/NUM_WIDTH height_ = height/NUM_HEIGHT # loop over the contours individually final_boxes = [] for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 1000: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates final_box = [] (tl, tr, br, bl) = box h = int(index/NUM_HEIGHT) w = index%NUM_WIDTH for coordinate in box: final_coordinate = (topLeft[0] - ((height_ * h) + coordinate[1]/IMAGE_HEIGHT*height_), topLeft[1] + ((width_ * w) + coordinate[0]/IMAGE_WIDTH*width_)) final_box.append(final_coordinate) #TODO: if box outside of bounding range filter out? final_boxes.append(final_box) for box in final_boxes: transformed_box = [] for point in box: # tb = pyproj.transform(p2, p1, point[0], point[1]) tb = [point[0].tolist(), point[1].tolist()] transformed_box.append({'lat': tb[0], 'lng': tb[1]}) transformed_boxes.append(transformed_box) return {'final_boxes': transformed_boxes}
def detect_size(imagefile, width=1, unit=0): #convert image to grayscale, and blur it slightly image = cv2.imread(imagefile) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: pixelsPerMetric = dB / 3.5 # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric # draw the object sizes on the image cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image cv2.imshow("Image", orig) cv2.waitKey(0)
def im(a): image = cv2.imread(args[a]) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Image1", gray) gray = cv2.GaussianBlur(gray, (7, 7), 0) cv2.imshow("Image2", gray) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) cv2.imshow("canny", edged) edged = cv2.dilate(edged, None, iterations=1) cv2.imshow("dilate", edged) edged = cv2.erode(edged, None, iterations=1) cv2.imshow("erode", edged) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) #print("{:.1f}px".format(dA)) #print("{:.1f}px".format(dB)) arr.append(dA) arr.append(dB) brr.append(tltrX) brr.append(tltrY) brr.append(trbrX) brr.append(trbrY) break
def ImgDistances(image, self): self.filter = "" gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # sort the contours from left-to-right and, then initialize the # distance colors and reference object (cnts, _) = contours.sort_contours(cnts) colors = ((0, 0, 255), (240, 0, 159), (0, 165, 255), (255, 255, 0), (255, 0, 255)) refObj = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) # compute the center of the bounding box cX = np.average(box[:, 0]) cY = np.average(box[:, 1]) # if this is the first contour we are examining (i.e., # the left-most contour), we presume this is the # reference object if refObj is None: # unpack the ordered bounding box, then compute the # midpoint between the top-left and top-right points, # followed by the midpoint between the top-right and # bottom-right (tl, tr, br, bl) = box (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # compute the Euclidean distance between the midpoints, # then construct the reference object D = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) refObj = (box, (cX, cY), D / float(self.tbDistance.text())) continue # draw the contours on the image orig = image.copy() cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) cv2.drawContours(orig, [refObj[0].astype("int")], -1, (0, 255, 0), 2) # stack the reference coordinates and the object coordinates # to include the object center refCoords = np.vstack([refObj[0], refObj[1]]) objCoords = np.vstack([box, (cX, cY)]) # loop over the original points for ((xA, yA), (xB, yB), color) in zip(refCoords, objCoords, colors): # draw circles corresponding to the current points and # connect them with a line cv2.circle(orig, (int(xA), int(yA)), 5, color, -1) cv2.circle(orig, (int(xB), int(yB)), 5, color, -1) cv2.line(orig, (int(xA), int(yA)), (int(xB), int(yB)), color, 2) # compute the Euclidean distance between the coordinates, # and then convert the distance in pixels to distance in # units D = dist.euclidean((xA, yA), (xB, yB)) / refObj[2] (mX, mY) = midpoint((xA, yA), (xB, yB)) cv2.putText(orig, "{:.1f}in".format(D), (int(mX), int(mY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.55, color, 2) # show the output image cv2.imshow("Image", orig) cv2.waitKey(0) cv2.destroyAllWindows() return
def openpose_overlay(self, subject_id: int, subject_data: dict, img_frame: np.ndarray, camera: str): for key, data in subject_data.items(): if key == 'pose' or key == 'face': for keypoint_idx, keypoint_values in data.items(): keypoint_x = round(keypoint_values[0] * VIDEO_RESOLUTION[camera]['x']) keypoint_y = round(keypoint_values[1] * VIDEO_RESOLUTION[camera]['y']) keypoint_c = round(keypoint_values[2] * 5) cv2.circle(img_frame, (keypoint_x, keypoint_y), keypoint_c, COLOR_MAP[subject_id], -1) # Connected joints if key == 'pose': keypoint_idx = int(keypoint_idx) if keypoint_idx in OPENPOSE_KEYPOINT_LINKS: for keypoint_link_idx in OPENPOSE_KEYPOINT_LINKS[ keypoint_idx]: pose_keypoints = subject_data['pose'] if str(keypoint_link_idx) in pose_keypoints: keypoint = pose_keypoints[str( keypoint_link_idx)] keypoint_link_x = round( keypoint[0] * VIDEO_RESOLUTION[camera]['x']) keypoint_link_y = round( keypoint[1] * VIDEO_RESOLUTION[camera]['y']) if keypoint_x == 0 or keypoint_y == 0 or keypoint_link_x == 0 or keypoint_link_y == 0: break cv2.line( img_frame, (keypoint_x, keypoint_y), (keypoint_link_x, keypoint_link_y), COLOR_MAP[subject_id], 1) elif key == 'expansiveness': if camera in data: expansiveness_data = data[camera] exp_xmin = round(expansiveness_data['x'][0] * VIDEO_RESOLUTION[camera]['x']) exp_xmax = round(expansiveness_data['x'][1] * VIDEO_RESOLUTION[camera]['x']) exp_ymin = round(expansiveness_data['y'][0] * VIDEO_RESOLUTION[camera]['y']) exp_ymax = round(expansiveness_data['y'][1] * VIDEO_RESOLUTION[camera]['y']) cv2.rectangle(img_frame, (exp_xmin, exp_ymax), (exp_xmax, exp_ymin), COLOR_MAP[subject_id]) elif key == 'overlap': overlay_alpha = 0.3 if camera in data: overlap_data = data[camera] vertices = vertices_from_polygon(overlap_data['polygon']) ovl_xmin = round(vertices['x']['min'] * VIDEO_RESOLUTION[camera]['x']) ovl_xmax = round(vertices['x']['max'] * VIDEO_RESOLUTION[camera]['x']) ovl_ymin = round(vertices['y']['min'] * VIDEO_RESOLUTION[camera]['y']) ovl_ymax = round(vertices['y']['max'] * VIDEO_RESOLUTION[camera]['y']) overlay = img_frame.copy() cv2.rectangle(overlay, (ovl_xmin, ovl_ymax), (ovl_xmax, ovl_ymin), COLOR_MAP['overlap'], -1) cv2.addWeighted(overlay, overlay_alpha, img_frame, 1 - overlay_alpha, 0, img_frame) elif key == 'intragroup_distance': overlay_alpha = 0.1 if camera in data: intragroup_distance_data = data[camera] intragroup_distance_center = intragroup_distance_data[ 'center'] intragroup_distance_polygon = intragroup_distance_data[ 'polygon'] center_x = round(intragroup_distance_center[0] * VIDEO_RESOLUTION[camera]['x']) center_y = round(intragroup_distance_center[1] * VIDEO_RESOLUTION[camera]['y']) cv2.drawMarker(img_frame, (center_x, center_y), COLOR_MAP['intragroup_distance'], markerType=cv2.MARKER_CROSS, markerSize=10, thickness=1) vertices = list() for point in intragroup_distance_polygon: point_x, point_y = point[0], point[1] point_x = round(point_x * VIDEO_RESOLUTION[camera]['x']) point_y = round(point_y * VIDEO_RESOLUTION[camera]['y']) point_int = [point_x, point_y] if point_int not in vertices: vertices.append(point_int) vertices = np.array(vertices, np.int32) try: vertices = np.array(order_points(vertices), np.int32) except ValueError: print(self.frame_idx, camera, vertices) overlay = img_frame.copy() cv2.fillPoly(overlay, [vertices], COLOR_MAP['intragroup_distance']) cv2.addWeighted(overlay, overlay_alpha, img_frame, 1 - overlay_alpha, 0, img_frame) elif key == 'center_interaction': if camera == SIDEVIEW_CAMERA: resolution = np.array( list(VIDEO_RESOLUTION[camera].values())) center_point = np.array(data['center']) center_point = np.rint( np.multiply(center_point, resolution)).astype(int) interaction_point = np.array(data['subject_point']) interaction_point = np.around( np.multiply(interaction_point, resolution)).astype(int) cv2.line(img_frame, (center_point[0], center_point[1] - 10), (center_point[0], center_point[1] + 10), COLOR_MAP['center_interaction'], 2) cv2.drawMarker(img_frame, tuple(interaction_point), COLOR_MAP[subject_id], markerType=cv2.MARKER_DIAMOND, markerSize=10, thickness=1) cv2.line(img_frame, tuple(interaction_point), tuple(center_point), COLOR_MAP[subject_id]) return img_frame
cv2.circle(frame, (point[0], point[1]), 2, (0, 0, 255), 2) cv2.imshow('Test Frame', frame) if len(points) == 4: mixer.music.load('instruction3.mp3') mixer.music.play() break if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows() # process of filtering the dots points = [[20, 30], [40, 20], [15, 300], [80, 40]] points = np.array(points, dtype="int32") res = perspective.order_points(points) res = res.reshape((-1, 1, 2)) #dots filtering ends above while 1: if intersectfrm != 0: intersectfrm += 1 df = pd.DataFrame(columns=['x', 'y']) midpoints = [] # grab a frame (grabbed, frame0) = vs.read() #markers = detect_markers(frame0)
def visual(image, cnt, pixelsPerMetric, width): # compute the rotated bounding box of the contour original = image.copy() rect = cv2.minAreaRect(cnt) rect = cv2.cv.BoxPoints(rect) if imutils.is_cv2() else cv2.boxPoints(rect) rect = np.array(rect, dtype="int") # order the contour as (top-left, top-right, bottom-right, bottom-left) rect = perspective.order_points(rect) cv2.drawContours(original, [rect.astype("int")], -1, (255, 0, 0), 1) # loop over the original points and draw them for (x, y) in rect: cv2.circle(original, (int(x), int(y)), 2, (255, 0, 0), -1) (topleft, topright, bottomright, bottomleft) = rect def mid(A, B): return ((A[0] + B[0]) / 2, (A[1] + B[1]) / 2) (topX, topY) = mid(topleft, topright) (bottomX, bottomY) = mid(bottomleft, bottomright) # compute the midpoint respectively (leftX, leftY) = mid(topleft, bottomleft) (rightX, rightY) = mid(topright, bottomright) # visual the middle-point cv2.circle(original, (int(topX), int(topY)), 2, (255, 0, 0), -1) cv2.circle(original, (int(bottomX), int(bottomY)), 2, (255, 0, 0), -1) cv2.circle(original, (int(leftX), int(leftY)), 2, (255, 0, 0), -1) cv2.circle(original, (int(rightX), int(rightY)), 2, (255, 0, 0), -1) # visual the boject lenth and width cv2.line(original, (int(topX), int(topY)), (int(bottomX), int(bottomY)), (255, 0, 0), 1) cv2.line(original, (int(leftX), int(leftY)), (int(rightX), int(rightY)), (255, 0, 0), 1) # compute the 2D EU distance between the midpoints dA = dist.euclidean((topX, topY), (bottomX, bottomY)) dB = dist.euclidean((leftX, leftY), (rightX, rightY)) # calculate the pixels per metric if pixelsPerMetric is None: pixelsPerMetric = dB / width # calculate the size lenA = dA / pixelsPerMetric lenB = dB / pixelsPerMetric # present cv2.putText(original, "{:.2f}cm".format(lenA), (int(topX - 15), int(topY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(original, "{:.2f}cm".format(lenB), (int(rightX + 10), int(rightY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) print(lenA, lenB) cv2.imshow("Image", original) cv2.waitKey(0)
def get_pixel_per_metric(img, width): # load the image, cvt it to grayscale and blurr it slightly image = cv2.imread(img) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) # perform edge detection, then perform a dilation + erosion to close gaps in between object edges edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in edged map cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left to right and initialize the 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None orig = image.copy() # loop over contours for c in cnts: # if contour is not sufficiently large...ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour box = cv2.minAreaRect(c) box = cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right and bottom-left # order, then draw the outline of rotated bounding box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the euclidian distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) # height dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # width # if pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric if pixelsPerMetric is None: pixelsPerMetric = dB / width return pixelsPerMetric
def Getsize(Image_Path): # construct the argument parse and parse the arguments ''' ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to the input image") ap.add_argument("-w", "--width", type=float, required=True, help="width of the left-most object in the image (in inches)") args = vars(ap.parse_args()) ''' # load the image, convert it to grayscale, and blur it slightly # image = cv2.imread(args["image"]) #image = cv2.imread("test_09201.jpg") image = cv2.imread(Image_Path) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 变成灰度 gray = cv2.GaussianBlur(gray, (7, 7), 0) # 高斯过滤器 # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, 80, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) # find contours in the edge map 边缘图中物体相对应的轮廓线。 cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # print(tlblX, tlblY) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: # pixelsPerMetric = dB / args["width"] pixelsPerMetric = dB / 2 # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric print(dimA) print(dimB) with open('R.txt', 'w') as f: f.write(str(dimA)) f.write('\n') f.write(str(dimB)) f.close() #return Size_all # draw the object sizes on the image cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image cv2.imshow("Image", orig) cv2.waitKey(1000)
for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 100: continue # compute the rotated bounding box of the contour orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right
def api_root(): preDima = 0 preDimb = 0 imagedata = request.json['b64'] image = base64.b64decode(imagedata) filename = 'some_image.jpg' with open(filename, 'wb') as f: f.write(image) image = cv2.imread('some_image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (7, 7), 0) edged = cv2.Canny(gray, 50, 100) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] (cnts, _) = contours.sort_contours(cnts) pixelsPerMetric = None for c in cnts: if cv2.contourArea(c) < 100: continue orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2) # loop over the original points and draw them for (x, y) in box: cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = midpoint(tl, tr) (blbrX, blbrY) = midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = midpoint(tl, bl) (trbrX, trbrY) = midpoint(tr, br) # draw the midpoints on the image cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if pixelsPerMetric is None: pixelsPerMetric = dB / 1.0 # compute the size of the object dimA = dA / pixelsPerMetric dimB = dB / pixelsPerMetric if preDima < dimA: preDima = dimA if preDimb < dimB: preDimb = dimB # draw the object sizes on the image cv2.putText(orig, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) cv2.putText(orig, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2) # show the output image final = str(preDima) + ',' + str(preDimb) return str(final)
def process_image(self, frame): gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.medianBlur(gray, 5) # Get Parameters from ROS interface MinThreshold = rospy.get_param('~MinThreshold') MaxThreshold = rospy.get_param('~MaxThreshold') MinAreaThreshold = rospy.get_param('~MinAreaThreshold') ReferenceMeasure = rospy.get_param('~ReferenceMeasure') # perform edge detection, then perform a dilation + erosion to # close gaps in between object edges edged = cv2.Canny(gray, MinThreshold, MaxThreshold) kernel = np.ones((3, 3), np.uint8) edged = cv2.dilate(edged, kernel, iterations=1) # find contours in the edge map cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the # 'pixels per metric' calibration variable (cnts, _) = contours.sort_contours(cnts) # compute the rotated bounding box of the contour processed_frame = frame.copy() # loop over the contours individually for c in cnts: # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < MinAreaThreshold: continue hull = cv2.convexHull(c, returnPoints=True) # compute the rotated bounding box of the contour box = cv2.minAreaRect(hull) box = cv2.cv.BoxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) cv2.drawContours(processed_frame, [ box.astype("int")], -1, (0, 0, 255), 2) # loop over the processed_frame points and draw them for (x, y) in box: cv2.circle(processed_frame, (int(x), int(y)), 5, (0, 0, 255), -1) # unpack the ordered bounding box, then compute the midpoint # between the top-left and top-right coordinates, followed by # the midpoint between bottom-left and bottom-right coordinates (tl, tr, br, bl) = box (tltrX, tltrY) = self.midpoint(tl, tr) (blbrX, blbrY) = self.midpoint(bl, br) # compute the midpoint between the top-left and top-right points, # followed by the midpoint between the top-righ and bottom-right (tlblX, tlblY) = self.midpoint(tl, bl) (trbrX, trbrY) = self.midpoint(tr, br) # if the contour is not sufficiently large, ignore it if cv2.contourArea(c) < 10000: # draw the midpoints on the frame cv2.circle(processed_frame, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1) cv2.circle(processed_frame, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1) cv2.circle(processed_frame, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1) cv2.circle(processed_frame, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1) # draw lines between the midpoints cv2.line(processed_frame, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2) cv2.line(processed_frame, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2) # compute the Euclidean distance between the midpoints dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) # if the pixels per metric has not been initialized, then # compute it as the ratio of pixels to supplied metric # (in this case, inches) if self.pixelsPerMetric is None: self.pixelsPerMetric = dB / ReferenceMeasure # compute the size of the object dimA = dA / self.pixelsPerMetric dimB = dB / self.pixelsPerMetric if cv2.contourArea(c) < 10000: # draw the object sizes on the frame cv2.putText(processed_frame, "{:.1f}mm".format(dimA), (int(tltrX - 15), int(tltrY - 10) ), cv2.FONT_HERSHEY_SIMPLEX, 0.70, (0, 0, 255), 2) cv2.putText(processed_frame, "{:.1f}mm".format(dimB), (int(trbrX + 10), int(trbrY) ), cv2.FONT_HERSHEY_SIMPLEX, 0.70, (0, 0, 255), 2) return processed_frame
def camera_callback(self, data): # Return if node is not enabled if (not self.is_node_enabled): return # Node is enabled, process the camera data try: cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8") except CvBridgeError as e: print(e) img = cv_image.copy() #img_org = cv2.imread('/home/mbzirc-01/Pictures/wrenches_blk_2.jpg') #img_org = cv2.imread('/home/mbzirc-01/Pictures/panel_query.JPG') #img = imutils.resize(img_org, width=640) output = img.copy() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) #thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)[1] thresh = cv2.adaptiveThreshold(blurred, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,3,2) # perform edge detection, then perform a dilation + erosion to close gaps in between object edges edged = cv2.Canny(blurred, 20, 150) edged = cv2.dilate(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) edged = cv2.erode(edged, None, iterations=1) edged2 = auto_canny(blurred) edged3 = cv2.dilate(edged2.copy(), None, iterations=1) edged4 = cv2.erode(edged3.copy(), None, iterations=1) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(4,50)) filled = cv2.morphologyEx(edged4, cv2.MORPH_CLOSE, kernel) """ cv2.imshow("thresh", thresh) cv2.waitKey(10) cv2.imshow("edged4", edged4) cv2.waitKey(10) cv2.imshow("filled", filled) cv2.waitKey(10) """ # find contours in the thresholded image and initialize the shape detector #cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cv2.findContours(filled.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # sort the contours from left-to-right and initialize the (cnts, _) = contours.sort_contours(cnts) #zlab = ToolLabeler() # loop over the contours simil = [] wr_contours = [] cr_contours = [] pnl_contour = [] dim_v = [] wr_count = 0 cr_count = 0 circleDetected = False toolIdentified = False for c in cnts: # compute the center of the contour, then detect the name of the # shape using only the contour M = cv2.moments(c) #cX = int((M["m10"] / M["m00"])) #cY = int((M["m01"] / M["m00"])) retSim = cv2.matchShapes(cnts_s[0],c,3,0.0) simil = np.hstack((simil,retSim)) # multiply the contour (x, y)-coordinates by the resize ratio, # then draw the contours and the name of the shape on the image c = c.astype("float") c *= 1 c = c.astype("int") text = "{}".format(shape) # if the contour is too large or too small, ignore it if cv2.contourArea(c) < 80 or cv2.contourArea(c) > 0.1*img.shape[0]*img.shape[1]: continue # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.01 * peri, True) (x, y, w, h) = cv2.boundingRect(approx) aspectRatio = w / float(h) #print(len(approx),aspectRatio) # compute the rotated bounding box of the contour #orig = image.copy() box = cv2.minAreaRect(c) box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box) box = np.array(box, dtype="int") # order the points in the contour such that they appear # in top-left, top-right, bottom-right, and bottom-left # order, then draw the outline of the rotated bounding # box box = perspective.order_points(box) #cv2.drawContours(output, [box.astype("int")], -1, (0, 255, 0), 2) #print('hello',[box.astype("int")]) (tl, tr, br, bl) = box dA = distance.euclidean(tl, bl) dB = distance.euclidean(tl, tr) keepRatio = aspectRatio > 0.1 and aspectRatio < 0.3 keepSimilarity = retSim > 0.8 Circle = len(approx)>8 and aspectRatio > 0.8 and aspectRatio < 1.2 if keepRatio and keepSimilarity: wr_count = wr_count + 1 #wr_contours.append(c) wr_contours = np.append(wr_contours,c) #wr_contours = np.concatenate((wr_contours,c)) cv2.drawContours(output, [c], -1, (0, 255, 0), 2) (t_x, t_y, t_w, t_h) = cv2.boundingRect(c) dim = (t_w, t_h) dim_v = np.append(dim_v,dim) size = self.label(np.array([t_w,t_h])) if size == self.tool_size: tool_contour = c toolIdentified = True if Circle: cr_count = cr_count + 1 cr_contours = np.append(cr_contours,c) #cv2.drawContours(output, [c], -1, (255, 0, 0), 2) #size = zlab.label(np.array([dA,dB])) #print(int(bl[0])) #cv2.putText(output, "({:d},{:d})".format(int(dA),int(dB)), (int(bl[0])-15,int(bl[1])+25), cv2.FONT_HERSHEY_SIMPLEX, 0.5, 0, 2) #cv2.putText(output, size, (int(bl[0])-15,int(bl[1])+55), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2) # show the output image cv2.imshow("out1", output) cv2.waitKey(10) wr_contours = np.reshape(wr_contours,(1,-1,2)) wr_contours = wr_contours.astype(int) (wrs_x, wrs_y, wrs_w, wrs_h) = cv2.boundingRect(wr_contours) cv2.rectangle(output, (wrs_x, wrs_y), (wrs_x + wrs_w, wrs_y + wrs_h), (255, 0, 0), 2) self.wrenches_ROI.x_offset = wrs_x self.wrenches_ROI.y_offset = wrs_y self.wrenches_ROI.width = wrs_w self.wrenches_ROI.height = wrs_h self.wrenches_ROI_pub.publish(self.wrenches_ROI) """ wrs_Rect = cv2.minAreaRect(wr_contours) wrs_Rect = cv2.cv.BoxPoints(wrs_Rect) if imutils.is_cv2() else cv2.boxPoints(wrs_Rect) wrs_Rect = np.array(wrs_Rect, dtype="int") wrs_Rect = perspective.order_points(wrs_Rect) (wrs_tl, wrs_tr, wrs_br, wrs_bl) = wrs_Rect wrs_dA = distance.euclidean(wrs_tl, wrs_bl) wrs_dB = distance.euclidean(wrs_tl, wrs_tr) cv2.drawContours(output, [wrs_Rect.astype("int")], -1, (255, 0, 0), 2) """ if cr_count == 1 : cr_contours = np.reshape(cr_contours,(1,-1,2)) cr_contours = cr_contours.astype(int) (vlv_x, vlv_y, vlv_w, vlv_h) = cv2.boundingRect(cr_contours) cv2.rectangle(output, (vlv_x, vlv_y), (vlv_x + vlv_w, vlv_y + vlv_h), (255, 0, 0), 2) self.valve_ROI.x_offset = vlv_x self.valve_ROI.y_offset = vlv_y self.valve_ROI.width = vlv_w self.valve_ROI.height = vlv_h self.valve_ROI_pub.publish(self.valve_ROI) # If the wrenches are detected, return the panel region of interest pnl_contour = np.append(wr_contours,cr_contours) pnl_contour = np.reshape(pnl_contour,(1,-1,2)) pnl_contour = pnl_contour.astype(int) (pnl_x, pnl_y, pnl_w, pnl_h) = cv2.boundingRect(pnl_contour) cv2.rectangle(output, (pnl_x, pnl_y), (pnl_x + pnl_w, pnl_y + pnl_h), (0, 0, 255), 2) #print(dim_v) rospy.loginfo("Found Panel") self.panel_ROI.x_offset = pnl_x self.panel_ROI.y_offset = pnl_y self.panel_ROI.width = pnl_w self.panel_ROI.height = pnl_h self.panel_ROI_pub.publish(self.panel_ROI) #result = WrenchDetectionResult() #result.ROI = [pnl_x, pnl_y, pnl_w, pnl_h] #self.server.set_succeeded(result) # Disable the node since it found its target #self.is_node_enabled = False if toolIdentified: (tool_x, tool_y, tool_w, tool_h) = cv2.boundingRect(tool_contour) cv2.rectangle(output, (tool_x, tool_y), (tool_x + tool_w, tool_y + tool_h), (255, 0, 255), 2) self.tool_ROI.x_offset = tool_x self.tool_ROI.y_offset = tool_y self.tool_ROI.width = tool_w self.tool_ROI.height = tool_h self.tool_ROI_pub.publish(self.tool_ROI) # show the output image cv2.imshow("out2", output) cv2.waitKey(10)