I have a video stream where I do the detection of people using Opencv and python.
My ROI is rectangular, but I would like to make a custom shape as in the figure.
It seems this is a stationary camera. If so, you can hard code the rectangular region of interest. You can then use a mask (created with for instance MS Paint) to black out everything outside of the custom shape.
Result:
Code:
import cv2
# load image
img = cv2.imread('image.jpg')
# load mask
mask = cv2.imread('roi_mask.png',0)
# create subimage
roi = img[120:350,150:580]
# mask roi
masked_roi = cv2.bitwise_and(roi,roi,mask=mask)
# display result
cv2.imshow('Roi',roi)
cv2.imshow('Mask',mask)
cv2.imshow('Result',masked_roi)
cv2.waitKey(0)
cv2.destroyAllWindows()
Related
I want to find the bright spots in the above image and tag them using some symbol. For this i have tried using the Hough Circle Transform algorithm that OpenCV already provides. But it is giving some kind of assertion error when i run the code. I also tried the Canny edge detection algorithm which is also provided in OpenCV but it is also giving some kind of assertion error. I would like to know if there is some method to get this done or if i can prevent those error messages.
I am new to OpenCV and any help would be really appreciated.
P.S. - I can also use Scikit-image if necessary. So if this can be done using Scikit-image then please tell me how.
Below is my preprocessing code:
import cv2
import numpy as np
image = cv2.imread("image1.png")
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
binary_image = np.where(gray_image > np.mean(gray_image),1.0,0.0)
binary_image = cv2.Laplacian(binary_image, cv2.CV_8UC1)
If you are just going to work with simple images like your example where you have black background, you can use same basic preprocessing/thresholding then find connected components. Use this example code to draw a circle inside all circles in the image.
import cv2
import numpy as np
image = cv2.imread("image1.png")
# constants
BINARY_THRESHOLD = 20
CONNECTIVITY = 4
DRAW_CIRCLE_RADIUS = 4
# convert to gray
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# extract edges
binary_image = cv2.Laplacian(gray_image, cv2.CV_8UC1)
# fill in the holes between edges with dilation
dilated_image = cv2.dilate(binary_image, np.ones((5, 5)))
# threshold the black/ non-black areas
_, thresh = cv2.threshold(dilated_image, BINARY_THRESHOLD, 255, cv2.THRESH_BINARY)
# find connected components
components = cv2.connectedComponentsWithStats(thresh, CONNECTIVITY, cv2.CV_32S)
# draw circles around center of components
#see connectedComponentsWithStats function for attributes of components variable
centers = components[3]
for center in centers:
cv2.circle(thresh, (int(center[0]), int(center[1])), DRAW_CIRCLE_RADIUS, (255), thickness=-1)
cv2.imwrite("res.png", thresh)
cv2.imshow("result", thresh)
cv2.waitKey(0)
Here is resulting image:
Edit: connectedComponentsWithStats takes a binary image as input, and returns connected pixel groups in that image. If you would like to implement that function yourself, naive way would be:
1- Scan image pixels from top left to bottom right until you encounter a non-zero pixel that does not have a label (id).
2- When you encounter a non-zero pixel, search all its neighbours recursively( If you use 4 connectivity you check UP-LEFT-DOWN-RIGHT, with 8 connectivity you also check diagonals) until you finish that region. Assign each pixel a label. Increase your label counter.
3- Continue scanning from where you left.
I am new to field of image processing in python opencv.
I want to remove the grid lines in the given image and show the output as a plain dark green image
I applied canny edge detection to detect the edges and then subtracting the image from edges . But its not showing the result as expected. Please suggest what can be done
Code
import numpy as np
import cv2
image = cv2.imread('Input image')
cv2.imshow("Original",image)
edges = cv2.Canny(image,100,200)
cv2.imshow("Canny",edges)
gray_image_RGB = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
print gray_image_RGB.shape[:]
cv2.imshow("Converted",gray_image_RGB)
outputimage=image-gray_image_RGB
cv2.imshow("Output",outputimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
I use python and openCV. I would like to separate the dart board from the background. I tried it with findContours and Canny edge detection but I couldn't make it.
Example image:
You can use grab-cut algorithm -
What you have to do is to specify the area of the image which'll act as a foreground as a rectangle. The algorithm will take a lil bit of time, and will throw out your required image... Code here requires a lil bit of tweaking though.
import numpy as np
import cv2
#a is your image
img = cv2.imread('a.jpg')
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (360,85,1670, 1900)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
cv2.imshow('image',img)
cv2.waitKey(0)
The final result in the source will give you a better result(after applying some masks)... But as I said you can modify it according to your wants.
Sources -
http://docs.opencv.org/3.1.0/d8/d83/tutorial_py_grabcut.html#gsc.tab=0
I need to find edge detection of medical images using OpenCV python .Which edge detector will be the best suited for my work? I have tried using canny Edge detector. I want to find edges of the medical images and find the histogram matching between two images.
Thanks in Advance:)
Can you post the images you're working on ? That will be better.
Also, you can try this code. It allows you to change the parameters of canny filters, Thresold 1 and thresold 2 and hence you will get an overall idea how you can apply canny filter to the image.
import cv2
import numpy as np
def nothing(x):
pass
#image window
cv2.namedWindow('image')
#loading images
img = cv2.imread('leo-messi-pic.jpg',0) # load your image with proper path
# create trackbars for color change
cv2.createTrackbar('th1','image',0,255,nothing)
cv2.createTrackbar('th2','image',0,255,nothing)
while(1):
# get current positions of four trackbars
th1 = cv2.getTrackbarPos('th1','image')
th2 = cv2.getTrackbarPos('th2','image')
#apply canny
edges = cv2.Canny(img,th1,th2)
#show the image
cv2.imshow('image',edges)
#press ESC to stop
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
As far as, histogram comparison is concerned. You can find all the histogram related cv2 APIs here.
http://docs.opencv.org/modules/imgproc/doc/histograms.html
Hope it helps.
I have an image of a face and I have used haar cascades to detect the locations (x,y,width,height) of the mouth, nose and each eye. I would like to set all pixels outside these regions to zero. What would be the fastest (computationally) way to do this? I'll eventually be doing it to video frames in real time.
I don't know whether it is the fastest way, but It is a way to do it.
Create a mask image with region of face as white, then apply bitwise_and function with original image and mask image.
x = y = 30
w = h = 100
mask = np.zeros(img.shape[:2],np.uint8)
mask[y:y+h,x:x+w] = 255
res = cv2.bitwise_and(img,img,mask = mask)
It takes 0.16 ms in my system (core i5,4GB RAM) for an image of size 400x300
EDIT - BETTER METHOD: You need not do as above. Simply create a zero image and then copy ROI from original image to zero image. that's all.
mask = np.zeros(img.shape,np.uint8)
mask[y:y+h,x:x+w] = img[y:y+h,x:x+w]
It takes only 0.032 ms in my system for above parameters, 5 times faster than above.
Results :
Input Image :
Output :
If a polygon ROI is to be made.
Create the polygon and make a mask for it. Multiply the image with the created frame.
ret,frame = cv2.imread()
xr=1
yr=1
# y,x
pts = np.array([[int(112*yr),int(32*xr)],[int(0*yr),int(623*xr)],[int(789*yr),int(628*xr)],[int(381*yr),int(4*xr)]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(frame,[pts],True,(0,255,255))
mask = np.zeros(frame.shape[:2],np.uint8)
cv2.fillPoly(mask,[pts],(255,255,255))
frame = cv2.bitwise_and(frame,frame,mask = mask)
cv2.imshow("masked frame", frame)