How to select ROI in binary threshold area using opencv in python? - python

I'm trying to crop the image from the binary image which is already processed from the original, suppose I have the original image
and I got the binary image from the original
and I want to crop the image only the white area using blob analysis
How can I do that?

In c++ you can use,
cv::Mat output_Mat = cv::Mat::zeros(RGB_Mat.size(), RGB_Mat.type());
RGB_Mat.copyTo(output_Mat, Binary_Mat);
Hope you can find corresponding python methods.

points = cv2.findNonZero(binary_image);
min_rect = cv2.boundingRect(points);

Related

Insert a masked region of an image in another (HDR) via OpenCV

I have two images and a mask. The first image (im1) is my source image, the second (im2) is the image whose region need to be inserted in im1 and the third image (mask) contains 1's in the region that needs to be pasted. All images have the same size (H*W*3). It should be noted that im1 is HDR( .exr format).
After reading via OpenCV
im1 = .imread(im1, cv2.IMREAD_ANYCOLOR | cv2.IMREAD_ANYDEPTH)[:,:,0:3]
im2 = ...
mask = ...
how can I transfer the masked region(contained in mask array) of image im2 without any loss of information (no change apart from masked region) in im1?
Normally you would use OpenCV's copyTo() method which will copy an image or masked image region from one Mat to another.
Unfortunately, this functionality is not available in the OpenCV Python bindings.
There is a Python workaround for this function from this answer though which you could use instead.

How to convert a 1 channel image into a 3 channel with PIL?

I have an image that has one channel. I would like duplicate this one channel such that I can get a new image that has the same channel, just duplicated three times. Basically, making a quasi RBG image.
I see some info on how to do this with OpenCV, but not in PIL. It looks easy in Numpy, but again, PIL is different. I don't want to get into the habit of jumping from library to library all the time.
Here's one way without looking too hard at the docs..
fake image:
im = Image.new('P', (16,4), 127)
Get the (pixel) size of the single band image; create a new 3-band image of the same size; use zip to create pixel tuples from the original; put that into the new image..
w, h = im.size
ima = Image.new('RGB', (w,h))
data = zip(im.getdata(), im.getdata(), im.getdata())
ima.putdata(list(data))
Or even possibly
new = im.convert(mode='RGB')
just use:
image = Image.open(image_info.path).convert("RGB")
can convert both 1-channel and 4-channel to 3-channel

Extract selected region from image with OpenCV

I have a region of an image selected, like this:
http://slideplayer.com/4593320/15/images/9/Intelligent+scissors+http%3A%2F%2Frivit.cs.byu.edu%2FEric%2FEric.html.jpg
and now, using OpenCV I would like to extract the region selected.
How could I do it? I have already researched but nothing useful got.
Thanks in advance.
First of all you have to import your pixel locations into the program and you have to create contour object using the points. I guess you know how to do this.
You can find from following link how to create contour object:
Creating your own contour in opencv using python
You can fill black using following code out of your selected image
black = np.zeros(img.shape).astype(img.dtype)
color = [1, 1, 1]
cv2.fillPoly(black, contours, color)
new_img = img * black
I guess you know (or find) how to crop after black out remaining image using contour pixels.

How to get title from an image in openCV python

I was wondering how to get a title from a image in OpenCV.
At the moment I have this:
#Load a color image in grayscale
img = cv2.imread('lena.jpg',0)
From here, I'd like to get the title from 'img' by doing something like
img.title()
but I don't find any method for doing this.
Any suggestion?
Thanks in advance.
You have set the name of the image, in which case you can store that and refer back to it in the future. There is no way of retriving it from the Mat object as all that stores is the data of the image itself.
instead of:
#Load a color image in grayscale
img = cv2.imread('lena.jpg',0)
save the file name first then use that wherever you need it
image_filename = 'lena.jpg'
img = cv2.imread(image_filename,0)
There is no direct method in opencv to extract the title from an image. After we load the image in opencv by "imread", the image will be transformed into arrays/matrices. Its all numericals(Christopher Nolan) stuff :P .
One way I can suggest is, you can find "contours" by applying some heuristics like averaging/mean/medium of Area, width, height etc. and also try applying "RLSA(Run Length Smoothing Algorithm)" on those classified contours.
Documention and Code for RLSA is here

Aligning and stitching images based on defined feature using OpenCV

I would like to create a panoramic image by combining 2 images in which the same feature, a plus sign.
I've used cv2.xfeatures2d.SIFT_create() to find keypoints in the image however it doesn't find the plus symbol very well. Is there some way I can improve this by making it search specifically for a plus-shaped feature?
import cv2
image1 = cv2.imread('example_image.png')
sift = cv2.xfeatures2d.SIFT_create()
kp = sift.detect(grey_image1, None)
kp_image = cv2.drawKeypoints(grey_image1, kp, None)
def showimage(image, name="No name given"):
cv2.imshow(name, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
return
showimage(kp_image)
The source image is here, second image to align is here. Here is the resulting image from the code above. This is an example of the desired output made using GIMP and manually aligning the two images (the second image will need to transformed to fit properly).`
NB I'm open to using other approaches outside of OpenCV/Python to solve this problem.

Categories

Resources