def getthresholdedimg(im): imghsv = cv.CreateImage(cv.GetSize(im), 8, 3) # Convert image from RGB to HSV cv.CvtColor(im, imghsv, cv.CV_BGR2HSV) imgthreshold = cv.CreateImage(cv.GetSize(im), 8, 1) cv.InRangeS(imghsv, cv.Scalar(23, 100, 100), cv.Scalar(25, 255, 255), imgthreshold) # catch the orange yellow blob return imgthreshold
def earthmoverdistance(hist1, hist2, h_bins, s_bins): #Define number of rows numRows = h_bins * s_bins sig1 = np.array((numRows, 3), dtype=float) sig2 = np.array((numRows, 3), dtype=float) for h in range(h_bins): for s in range(s_bins): bin_val = cv2.QueryHistValue_2D(hist1, h, s) cv2.Set2D(sig1, h * s_bins + s, 0, cv2.Scalar(bin_val)) cv2.Set2D(sig1, h * s_bins + s, 1, cv2.Scalar(h)) cv2.Set2D(sig1, h * s_bins + s, 2, cv2.Scalar(s)) bin_val = cv2.QueryHistValue_2D(hist2, h, s) cv2.Set2D(sig2, h * s_bins + s, 0, cv2.Scalar(bin_val)) cv2.Set2D(sig2, h * s_bins + s, 1, cv2.Scalar(h)) cv2.Set2D(sig2, h * s_bins + s, 2, cv2.Scalar(s)) #This is the important line were the OpenCV EM algorithm is called return cv2.CalcEMD2(sig1, sig2, cv2.CV_DIST_L2)
import numpy as np import cv2 lower_color_bounds = cv2.Scalar(100, 0, 0) upper_color_bounds = cv2.Scalar(225, 80, 80) frame = cv2.imread('octo_first_layer.PNG', 1) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) mask = cv2.inRange(frame, lower_color_bounds, upper_color_bounds) mask_rgb = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) frame = frame & mask_rgb cv2.imshow('Frame', frame) cv2.waitKey(0)
def main(): inputFile, outputFile = (sys.argv[1], sys.argv[2]) im = cv2.imread(inputFile) cv2.Scalar(200, 200, 200) wim = cv2.inRange(im, np.array([200, 200, 200]), np.array([255, 255, 255])) cv2.imwrite(filename=outputFile, img=wim)
if __name__ == "__main__": orig = cv.LoadImage( r"C:\git\Python-Snippets\Image Recognition\images\D2C-Logins - RunDate 2019-10-03 - Part (22).image" ) #Convert in black and white res = cv.CreateImage(cv.GetSize(orig), 8, 1) cv.CvtColor(orig, res, cv.CV_BGR2GRAY) #Operations on the image openCloseImage(res) dilateImage(res, 2) erodeImage(res, 2) smoothImage(res, 5) thresholdImage(res, 150, cv.CV_THRESH_BINARY_INV) #Get contours approximated contourLow = getContours(res, 3) #Draw them on an empty image final = cv.CreateImage(cv.GetSize(res), 8, 1) cv.Zero(final) cv.DrawContours(final, contourLow, cv.Scalar(255), cv.Scalar(255), 2, cv.CV_FILLED) cv.ShowImage("orig", orig) cv.ShowImage("image", res) cv.SaveImage("modified.png", res) cv.ShowImage("contour", final) cv.SaveImage("contour.png", final) cv.WaitKey(0)
import glob import os import cv2 if not os.path.exists('label'): os.mkdir('label') white_low = cv2.Scalar(0,0,0) white_high = cv2.Scalar(0,0,255) yellow_low = cv2.Scalar(20,100,100) yellow_high = cv2.Scalar(30,255,255) path = "data\\*.jpg" for file in glob.glob(path): raw = cv2.imread(file) hsv = cv2.cvtColor(raw, cv2.COLOR_BGR2HSV) white_mask = cv2.inRange(hsv, white_low, white_high) yellow_mask = cv2.inRange(hsv, yellow_low, yellow_high) mask = white_mask + yellow_mask filename = os.path.splittext(file)[0] cv2.imwrite("label\\"+filename+".png", 255*mask)