Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
From a given image i'm able to create a binary mask that detect certain objects, how i can draw multiple rectangles a round those detected objects so that i're draw it to the original image also if it possible to obtain the corrdinates of those rectangle so i can plot them in the original image
As you haven't provide code, I will answer without code as well.
You should use findCountours. There is an opencv tutorial that helps you in this exact task: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_contours/py_contour_features/py_contour_features.html
cv2.findContours returns an array of contours, for each contour in contour you will need to:
x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am making google's t-rex runner game. So I got a .png file which contains all the fonts, picuters of the t-rex, etc. So I am now having problem in getting a part of the image. Here is the image:
Is there anyway in which I can get a part of the image. I am using python, pygame.
Assuming you know what a Rect and Surface is, here is a way to do it:
def image_at(png, rectangle): # rectangle would be the section you want in the png
rect = pygame.Rect(rectangle)
image = pygame.Surface(rect.size)
image.blit(png, (0, 0), rect)
return image
(inspired by https://www.pygame.org/wiki/Spritesheet which could be exactly what you were looking for)
Basically you create a surface with the dimensions of your desired section of the png, they you draw (blit) the whole png on your created surface, and that will show only the part you selected throught your rectangle.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Actually I work on a car detection project at real time, when I launch the camera I want to make a screenshot (.png) for any car and save it in a path, which means i need a function in opencv that does the screenshot of just a car detected (not all windows).
Suppose you have the following image:
Now i want to extract to individual images each of the independent cars, like this for example:
And save in path/image_date.png.
Thanks in advance.
Given an image and a bounding box that is determined by your car detector:
cv::Mat image = …;
cv::Rect roi = …; // roi stands for "region of interest"
cv::Mat output = image(roi); // slice the car rectangle out of the image
cv::imwrite("car.png", output);
Note that this operator() will reference the original image data, no data copy will be made. So this is a very efficient way of working on a specific region of interest.
How to best detect the cars in an image in the first place, is too broad for a SO question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I would like compare a frame of video with another image but i don't know how can i do it with python.
Someone can help me please
You can use various metrics, look them up to see how they're calculated and when you should use them. In Python this can be achieved easily with scikit-image.
import cv2
from skimage.measure import compare_mse, compare_nrmse, compare_ssim, compare_psnr
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')
# mean squared error
compare_mse(img1, img2)
# normalized root-mean-square
compare_nrmse(img1, img2)
# peak signal-to-noise ratio
compare_psnr(img1, img2)
# structural similarity index
compare_ssim(img1, img2, multichannel=True)
The images must have the same size.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to transform my RGB image to grayscale image by not using converting function but with the red green blue values . For example, if my image is totally blue, it will be converted to white if I get blue components of it and it will be black if I get red components of my RGB image. It will be done in Python via OpenCV.
Thanks in advance.
The converting function that you are referring to does the same - it weights the R,G and B channel values of each pixel, and takes the sum. Since OpenCV uses the BGR colorspace on reading images, your conversion function will be something like this-
def rgbToGray(img):
grayImg = 0.0722*img(:,:,1) + 0.7152*img(:,:,2) + 0.2126*img(:,:,3)
return grayImg
The specific weights mentioned here are taken from the ITU-R BT.709 standard used for HDTV, developed by the ATSC (https://en.wikipedia.org/wiki/Grayscale)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How to visualize the image gradient of this picture in Python, like the pic above?
Use the Pyton Imaging Library PIL (http://www.pythonware.com/products/pil/)
Load the image using Image.open and read the colors of each pixel into a 2D list using Image.getpixel (http://effbot.org/imagingbook/image.htm). One of the three RGB values will do because the image is grayscale thus the R, B, and G values of each pixel are equal to each other.
Calculate the gradient for each pixel except of those at the edge:
grad[x][y] = [(list[x+1][y]-list[x-1][y])/2.0, (list[x][y+1]-list[x][y-1])/2.0]
(Note that the gradient is a 2D vector. It has an x and a y value.)
Create a quiver plot e.g. with MatPlotLib's PyPlot (https://www.getdatajoy.com/examples/python-plots/vector-fields)
Also check this document where a gradient function from numpy is used: http://elektromagnetisme.no/2011/09/12/calculating-the-gradient-in-python/