Related
I have a sample image:
and I use different thresholding methods in order to count the number of pixels.
First Method is simple thresholding since on the source image I only have a one colored object against a white background.
Mat image = imread("/$image_path", IMREAD_GRAYSCALE);
Mat binary_image;
threshold(image, binary_image, 120, 255, THRESH_BINARY);
int TotalNumberOfPixels = binary_image.rows * binary_image.cols;
int PixelCount = TotalNumberOfPixels - cv::countNonZero(binary_image);
return PixelCount;
The second method is assuming I have an image with multiple colored objects (ie multiple colored marks) hence I need to filter and apply a red mask. I did it via:
Mat image2 = imread("/$image_path", IMREAD_COLOR);
Mat blurred, edge;
Mat bgrInv = ~image2;
Mat hsvIm;
Mat maskRed;
cvtColor(bgrInv, hsvIm, COLOR_BGR2HSV);
inRange(hsvIm, Scalar(80, 70, 239), Scalar(100, 255, 255), maskRed);
imshow("Mask", maskRed);
//blur(maskRed, blurred, Size(3, 3));
//Canny(blurred, edge, 75, 200, 3);
cout << "Pixel Count: " << countNonZero(maskRed)<< endl;
The output for both methods are:
Method 1: 406
Method 2: 155
I will be operating on a colored image hence I was using the second method at first. But I do not know if it will be "accurate" or correct.
Here is the sample template that I am working on. Its basically a survey type template with minor colored blocks. With red circles as mark placeholders for easier pre/post processing.
Let's have a look at the outputs of your two methods.
This is binary_image from method #1:
You count - cumbersomely (is this proper English?) - the black pixels, which corresponds to your task to count the red pixels. (By the way, invert the threshold and just count the white pixels.)
This is edge from method #2:
You count the white pixels, but as you can see, this is only the outline of the initial red object. And this does NOT correspond to your original task.
So, given both methods as they are, the first is more "accurate", at the moment and for the given example. Nevertheless, you mentioned objects of different colors, so method #2 should be reworked to count the proper pixels.
Could you please give examples for images with multiple objects of different color?
Also, I edited your question. (The edit isn't reviewed, yet.) The image loading part is important, since I guess, in method #1 you used imread(..., IMREAD_GRAYSCALE) and imread(..., IMREAD_COLOR) in method #2.
I think all methods are not accurate if your image is blured (such as in JPEG format). But let's assume it's clear.
To count colored object pixels, we can count all colored pixels or count all RED pixels.
(1) Find the colored regions in HSV:
How to detect colored patches in an image using OpenCV?
# count colored pixels in S(HSV)
def countColored(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
num = np.sum(s>20)
return num
(2) Find the Red regions:
How to find the RED color regions using OpenCV?
def countRed(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
mask1 = cv2.inRange(hsv, (0,50,20), (5,255,255))
mask2 = cv2.inRange(hsv, (175,50,20), (180,255,255))
mask = cv2.bitwise_or(mask1, mask2 )
num = cv2.countNonZero(mask)
return num
#!/usr/bin/python3
# 2019/02/28
import cv2
import numpy as np
def cvshow(img):
cv2.imshow("OpenCV", img)
cv2.waitKey();cv2.destroyAllWindows()
def countColored(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
num = np.sum(s>20)
return num
def countRed(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
mask1 = cv2.inRange(hsv, (0,50,20), (5,255,255))
mask2 = cv2.inRange(hsv, (175,50,20), (180,255,255))
mask = cv2.bitwise_or(mask1, mask2 )
num = cv2.countNonZero(mask)
return num
if __name__ == "__main__":
fpath = "fQipc.jpg"
img = cv2.imread(fpath)
num1 = countColored(img)
num2 = countRed(img)
print(num1, num2)
# 643, 555
Apologies in advance as i am newbie to OpenCV-Python. I set myself a task to create a Passport type image from the video capture.
Using a head and shoulders Haar Cascade i was able to create a portrait photo but i now want to turn the background to a white background (leaving the head and shoulders portrait in the foreground).
Just not sure how/ best way to do this. Any help would be welcome.
Many thanks in advance.
Here is the code:
import numpy as np
import cv2
# face file
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# eye file
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
# head shoulders file
hs_cascade = cv2.CascadeClassifier('HS.xml')
cap = cv2.VideoCapture(1)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
headshoulders = hs_cascade.detectMultiScale(gray, 1.3, 3)
# find the head and shoulders
for (x,y,w,h) in headshoulders:
# variable change to make portrait orientation
x = int(x*1.5)
w = int(w/1.5)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
# crop the image
crop_img = img[y: y + h, x: x + w]
# show original and crop
cv2.imshow('crop', crop_img)
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
elif k == ord('s'):
# save out the portrait image
cv2.imwrite('cropimage.png',crop_img)
# release the camera
cap.release()
cv2.destroyAllWindows()
I got it to work. Here is my solution.
PLEASE NOTE: This worked for HI-RES images (Nikon D7100 - JPEG). LOW-RES did NOT work when i tried a Webcam (Logitech C615).
I used some of the code from a link that was suggested.
# import numpy
import numpy as np
# import cv2
import cv2
# import Matplitlib
from matplotlib import pyplot as plt
# Fill any holes function
def get_holes(image, thresh):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
im_bw = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)[1]
im_bw_inv = cv2.bitwise_not(im_bw)
im_bw_inv, contour, _ = cv2.findContours(im_bw_inv, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contour:
cv2.drawContours(im_bw_inv, [cnt], 0, 255, -1)
nt = cv2.bitwise_not(im_bw)
im_bw_inv = cv2.bitwise_or(im_bw_inv, nt)
return im_bw_inv
# Remove background Function
def remove_background(image, thresh, scale_factor=.25, kernel_range=range(1, 15), border=None):
border = border or kernel_range[-1]
holes = get_holes(image, thresh)
small = cv2.resize(holes, None, fx=scale_factor, fy=scale_factor)
bordered = cv2.copyMakeBorder(small, border, border, border, border, cv2.BORDER_CONSTANT)
for i in kernel_range:
#kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*i+1, 2*i+1))
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (2*i+1, 2*i+1))
bordered = cv2.morphologyEx(bordered, cv2.MORPH_CLOSE, kernel)
unbordered = bordered[border: -border, border: -border]
mask = cv2.resize(unbordered, (image.shape[1], image.shape[0]))
fg = cv2.bitwise_and(image, image, mask=mask)
return fg
# Load a color image in grayscale
img = cv2.imread('original/11.png')
# Start background removal -- Parameters are <image> and <threshold level>
nb_img = remove_background(img, 180)
# Change Black Pixels to WHITE
nb_img[np.where((nb_img==[0,0,0]).all(axis=2))] = [255,255,255]
# resize the viewing size (as the images are too big for the screen
small = cv2.resize(nb_img, (300, 400))
# Show the finished image
cv2.imshow('image',small)
k = cv2.waitKey(0) & 0xFF
if k == 27: #wait for ESC key to exit
# if ESC pressed close the camera windows
cv2.destroyAllWindows()
elif k == ord('s'): #wait for 's' key to save and exit
# Save the img(greyscale version)
cv2.imwrite('bg_removal/11.png',small)
cv2.destroyAllWindows()
I want to find the HSV value of a LASER dot using opencv and python. I got the code http://opencv-srf.blogspot.com.au/2010/09/object-detection-using-color-seperation.html from here but it is in c++, installing visual studio and opencv takes time so i changed the code in python
import cv2
import numpy as np
def callback(x):
pass
cap = cv2.VideoCapture(0)
cv2.namedWindow('image')
ilowH = 0
ihighH = 179
ilowS = 0
ihighS = 255
ilowV = 0
ihighV = 255
# create trackbars for color change
cv2.createTrackbar('lowH','image',ilowH,179,callback)
cv2.createTrackbar('highH','image',ihighH,179,callback)
cv2.createTrackbar('lowS','image',ilowS,255,callback)
cv2.createTrackbar('highS','image',ihighS,255,callback)
cv2.createTrackbar('lowV','image',ilowV,255,callback)
cv2.createTrackbar('highV','image',ihighV,255,callback)
while(1):
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
cv2.imshow('hsv', hsv)
lower_hsv = np.array([ilowH, ilowS, ilowV])
higher_hsv = np.array([ihighH, ihighS, ihighV])
mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
cv2.imshow('mask', mask)
cv2.imshow('frame', frame)
print ilowH, ilowS, ilowV
if(cv2.waitKey(1) & 0xFF == ord('q')):
break
cv2.destroyAllWindows()
cap.release()
but this code doesnot threshold anything. It seems like the trackbars i created doesnot change the value of ilowH ,ilowS, ilowV . I checked it by printing those values inside while loop. What could be the problem for not thresholding any of those values or is there better code in python to find HSV values of the LASER.
Thank you, any help is appreciated.
You can grab the trackbar values with cv2.getTrackbarPos(). Also note that sometimes it puts trackbars out of order, which is annoying, but at least they're labeled.
However, I don't think that these trackbars will work very well for live video feed. There's a lot of freezing issues. You'll have to have a super low framerate (works for me with cv2.waitKey(500) if you're actually trying to display it). This is mostly due to the trackbars sucking, not the thresholding operation, which is not that slow.
You need to add your trackbars after you create the named window. Then, for your while loop, try:
while True:
# grab the frame
ret, frame = cap.read()
# get trackbar positions
ilowH = cv2.getTrackbarPos('lowH', 'image')
ihighH = cv2.getTrackbarPos('highH', 'image')
ilowS = cv2.getTrackbarPos('lowS', 'image')
ihighS = cv2.getTrackbarPos('highS', 'image')
ilowV = cv2.getTrackbarPos('lowV', 'image')
ihighV = cv2.getTrackbarPos('highV', 'image')
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_hsv = np.array([ilowH, ilowS, ilowV])
higher_hsv = np.array([ihighH, ihighS, ihighV])
mask = cv2.inRange(hsv, lower_hsv, higher_hsv)
frame = cv2.bitwise_and(frame, frame, mask=mask)
# show thresholded image
cv2.imshow('image', frame)
k = cv2.waitKey(1000) & 0xFF # large wait time to remove freezing
if k == 113 or k == 27:
break
and finally end the file with a cv2.destroyAllWindows()
As an aside, the maximum H value for HSV is 180, not 179.
Shameless plug: I happened to just finish a project doing precisely this, but on images. You can grab it on GitHub here. There is an example; try running it and then modifying as you need. It will let you change the colorspace and threshold inside each different colorspace, and it will print the final thresholding values that you ended on. Additionally it will return the output image from the operation for you to use, too. Hopefully it is useful for you! Feel free to send any issues or suggestions through GitHub for the project.
Here is an example of it running:
And as output it gives you:
Colorspace: HSV
Lower bound: [68.4, 0.0, 0.0]
Upper bound: [180.0, 255.0, 255.0]
as well as the binary image. I am currently working on getting this into a web application as well, but that probably won't be finished for a few days.
Use this code to find range of masking of real-time video! this might save you time. Below is a whole code, Check it and run it to have a test.
import cv2
import numpy as np
camera = cv2.VideoCapture(0)
def nothing(x):
pass
cv2.namedWindow('marking')
cv2.createTrackbar('H Lower','marking',0,179,nothing)
cv2.createTrackbar('H Higher','marking',179,179,nothing)
cv2.createTrackbar('S Lower','marking',0,255,nothing)
cv2.createTrackbar('S Higher','marking',255,255,nothing)
cv2.createTrackbar('V Lower','marking',0,255,nothing)
cv2.createTrackbar('V Higher','marking',255,255,nothing)
while(1):
_,img = camera.read()
img = cv2.flip(img,1)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hL = cv2.getTrackbarPos('H Lower','marking')
hH = cv2.getTrackbarPos('H Higher','marking')
sL = cv2.getTrackbarPos('S Lower','marking')
sH = cv2.getTrackbarPos('S Higher','marking')
vL = cv2.getTrackbarPos('V Lower','marking')
vH = cv2.getTrackbarPos('V Higher','marking')
LowerRegion = np.array([hL,sL,vL],np.uint8)
upperRegion = np.array([hH,sH,vH],np.uint8)
redObject = cv2.inRange(hsv,LowerRegion,upperRegion)
kernal = np.ones((1,1),"uint8")
red = cv2.morphologyEx(redObject,cv2.MORPH_OPEN,kernal)
red = cv2.dilate(red,kernal,iterations=1)
res1=cv2.bitwise_and(img, img, mask = red)
cv2.imshow("Masking ",res1)
if cv2.waitKey(10) & 0xFF == ord('q'):
camera.release()
cv2.destroyAllWindows()
break`
Thanks!
Hugs..
I want to extract only the rectangular part of the mouth detected by my code how can I do it:
import numpy as np
import cv2
face_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml')
mouth_cascade = cv2.CascadeClassifier('/usr/local/share/OpenCV/haarcascades/haarcascade_smile.xml')
img = cv2.imread('Images/image_0033.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
mouth = mouth_cascade.detectMultiScale(roi_gray,2.0,25)
for (ex,ey,ew,eh) in mouth:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),3)
cv2.imshow('img',nwimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
As seen in the code I just want to extract the rectangular mouth region I have used commands like var = img[y:y+h,x:x+w] but this has not worked.
It is simple, replace this line:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),3) with
cv2.rectangle(img,(ex,ey),(ex+ew,ey+eh),(0,255,0),3)
and display the following:
cv2.imshow('Detected Mouth',img)
In this way you will draw a rectangle over the mouth.
EDIT
You can crop your region of interest (in this case the mouth) using numpy operation as follows:
crop_img = img[ey:ey+eh, ex:ex+ew]
cv2.imshow('Cropped Mouth',crop_img)
This is what I got:
Sample 1:
Sample 2:
I took the code from this post:
how to crop the detected face in opencv and save roi as image in opencv python
The problem is that when I run this code, it shows a grey screen instead of showing the video from webcam.
Here's my coding:
import cv2
import os, sys
TRAINSET = "haarcascade_frontalface_default.xml"
DOWNSCALE = 4
cam = cv2.VideoCapture(0) #capture a video
cv2.namedWindow("preview")
classifier = cv2.CascadeClassifier(TRAINSET)
Compare_images=[]
for file in os.listdir("D:/Python code"):
if file.endswith(".jpg"):
Compare_images.append(file)
while True: # try to get the first frame
_, frame = cam.read()
key = cv2.waitKey(20)
if(key==32):
print "Name of Image:"
n= raw_input()
value=len(Compare_images)
cv2.imwrite('images/image'+str(n)+'.jpg', frame)
saved_image=cv2.imread("images/image"+str(n)+".jpg")
minisize = (saved_image.shape[1]/DOWNSCALE,saved_image.shape[0]/DOWNSCALE)
miniframe = cv2.resize(saved_image, minisize)
faces = classifier.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v*DOWNSCALE for v in f ]
print x
print y,w,h
x0,y0=int(x),int(y)
x1,y1=int(x+w),int(y+h)
print x0,y0,y1,y0
image = cv2.rectangle(saved_image, (x0,y0), (x1,y1), (0,0,255),2)
roi=saved_image[y0:y1,x0:x1]#crop
cv2.imwrite('roi.jpg',roi)
cv2.imshow("adsa", saved_image)
cv2.putText(frame, "Press ESC to close.", (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("preview", frame)
I'm really new into python, and I would really appreciate it if you are able to help!