import cv2 import numpy as np from skimage import morphology, color import matplotlib.pyplot as plt from scipy.spatial import distance as dist from imutils import perspective from imutils import contours import argparse import imutils def midpoint(ptA, ptB): return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
img = cv2.imread('F:\Pycode\ADAP_ANALYZER\kk.jpg')
img = cv2.resize(img,(1280, 1024))
lowerb = np.array([0, 0, 120]) upperb = np.array([200, 100, 255]) red_line = cv2.inRange(img, lowerb, upperb)
red_line = (255-red_line)
red_line = cv2.GaussianBlur(red_line, (5, 5), 0) ret, red_line = cv2.threshold(red_line, 45, 255, cv2.THRESH_BINARY)
red_line = cv2.Canny(red_line, 50, 100)alc
red_line = cv2.dilate(red_line, None, iterations=1) kernel = np.ones((10,10),np.uint8) red_line = cv2.erode(red_line, kernel, iterations=1)
red_line = cv2.morphologyEx(red_line, cv2.MORPH_TOPHAT, kernel)
red_line =morphology.medial_axis(red_line)
cv2.imwrite("F:\Pycode\ADAP_ANALYZER\yy.jpg",red_line) nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(red_line, connectivity=8) sizes = stats[1:, -1]; nb_components = nb_components - 1
minimum size of particles we want to keep (number of pixels)
here, it's a fixed value, but you can set it as you want, eg the mean of the sizes or whatever
min_size = 1800
your answer image
img2 = np.zeros((output.shape))
for i in range(0, nb_components): if sizes[i] >= min_size: img2[output == i + 1] = 255
cv2.imwrite("F:\Pycode\ADAP_ANALYZER\xx.jpg",img2) cv2.imshow('red', img2) cv2.waitKey(0)
-----------taken from other edgedetect program --------------------
image = cv2.imread("F:\Pycode\ADAP_ANALYZER\xx.jpg")
image = cv2.resize(image, (512, 256))
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0)
threshold the image, then perform a series of erosions +
dilations to remove any small regions of noise
f,thresh = cv2.threshold(gray, 70, 255, cv2.THRESH_BINARY)
cv2.imshow("image",thresh)
thresh = cv2.erode(thresh, None, iterations=1) thresh = cv2.dilate(thresh, None, iterations=1)
find contours in thresholded image, then grab the largest
one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts)
rows,cols = thresh.shape[:2]
[vx,vy,x,y] = cv2.fitLine(cnts, cv2.DIST_L2,0,0.01,0.01)
lefty = int((-x*vy/vx) + y)
righty = int(((cols-x)*vy/vx)+y)
cv2.line(thresh,(cols-1,righty),(0,lefty),(0,255,0),2)
c = max(cnts, key=cv2.contourArea)
for var in c:
with open('c:\your_file.txt', 'a') as f:
f.write(str(var) + "\n")
print(var)
for contour in cnts: perimeter = cv2.arcLength(contour, True) print(perimeter/2)
determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0]) extRight = tuple(c[c[:, :, 0].argmax()][0]) extTop = tuple(c[c[:, :, 1].argmin()][0]) extBot = tuple(c[c[:, :, 1].argmax()][0])
print(extLeft)
print(extRight)
print(extTop)
print(extBot)
draw the outline of the object, then draw each of the
extreme points, where the left-most is red, right-most
is green, top-most is blue, and bottom-most is teal
cv2.drawContours(image, [c], -1, (0, 255, 255), 2)
# show the output image
cv2.imshow("Image", image) cv2.waitKey(0)