Python OpenCv, only read part of image - python

I have thousands of large .png images (screenshots). I'm using opencv to do image recognition on a small portion of each image. I'm currently doing:
image = cv2.imread(path)
x,y,w,h = bounds
image = image[y:y + h, x:x + w]
The profiler tells me cv2.imread is a bottleneck. I'm wondering if I can make the script faster by only reading the part of each image I'm interested in rather than loading the entire image and then cropping to the bounds. I can't find an OpenCV flag for that though. Am I missing one?

AFAICT, there's no way to do this with OpenCV. But I did find a solution here: Load just part of an image in python
Simply using PIL to save the cropped region of interest when generating the screenshots works.

Related

Process image using opencv

I have a number of microscopic images and I want to apply detection algorithms on those images using OpenCV. For that, I need to apply some image processing methods. But I am not sure which will be the best method for this one.
I applied exposure and contrast from Shotwell (ubuntu software) which works perfectly fine with my case. How I can do the same in OpenCV or any image processing library in python?
I have attached a link to the input and the required image.
Input image: https://drive.google.com/file/d/1XoY8u5yk0X4620alR61GhoJrd9pJBYji/view?usp=sharing
Required image: https://drive.google.com/file/d/13DpbDYdNsOAh1k_ZLHpYV99ddlIF47vB/view?usp=sharing
For example
img=cv2.imread('points.jpg')
hsv_image = cv2.cvtColor(img ,cv2.COLOR_BGR2HSV)
gray = cv2.cvtColor(hsv_image,cv2.COLOR_BGR2GRAY)
_,mask = cv2.threshold(gray, 75, 255, cv2.THRESH_BINARY)
I get
But I'm not sure if I lost some points or added new ones.

Enlarge picture using Pillow package keep ratio

I am trying to use python pillow package to enlarge picture and here is my try
from PIL import Image
image = Image.open('Sample.jpg')
new_image = image.resize((1080, 1080))
new_image.save('Output.jpg')
The code is working but the output image loses completely the ratio. How can I enlarge the image without losing the quality?
What you're talking about is image upscaling, which is a more complex problem just than enlarging the image. I recommend looking into image upscaling packages. A lot of them use CNNs for this.

How to remove noise from an image using pillow?

I am trying to de-noise an image that I've made in order to read the numbers on it using Tesseract.
Noisy image.
Is there any way to do so?
I am kind of new to image manipulation.
from PIL import ImageFilter
im1 = im.filter(ImageFilter.BLUR)
im2 = im.filter(ImageFilter.MinFilter(3))
im3 = im.filter(ImageFilter.MinFilter)
The Pillow library provides the ImageFilter module that can be used to enhance images. Per the documentation:
The ImageFilter module contains definitions for a pre-defined set of filters, which can be be used with the Image.filter() method.
These filters work by passing a window or kernel over the image, and computing some function of the pixels in that box to modify the pixels (usually the central pixel)
The MedianFilter seems to be widely used and resembles the description given in nishthaneeraj's answer.
You have to read Python pillow Documentation
Python pillow Documentation link:
https://pillow.readthedocs.io/en/stable/
Pillow image module:
https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html#module-PIL.ImageFilter
How do you remove noise from an image in Python?
The mean filter is used to blur an image in order to remove noise. It involves determining the mean of the pixel values within a n x n kernel. The pixel intensity of the center element is then replaced by the mean. This eliminates some of the noise in the image and smooths the edges of the image.

How to make shadowed part of background count as background (picture below) with OpenCV in Python?

I am very new to OpenCV(and to StackOverflow). I'm writing a program with OpenCV which takes a picture with an object (i.e. pen(rice, phone) put on paper) and calculates what percent does the object make of the picture.
Problem I'm facing with is when I threshold image (tried adaptive and otsu) photo is a little bit shadow around edges:
Original image
Resulted picture
And here's my code:
import cv2
img = cv2.imread("image.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
b,g,r = cv2.split(img)
th, thresh = cv2.threshold(b, 100, 255, cv2.THRESH_BINARY|cv2.THRESH_OTSU)
cv2.imwrite("image_bl_wh.png", thresh)
Tried to blur and morphology, but couldn't do it.
How can I make my program count that black parts around the picture as background and is there more better and easier way to do it?
P.S. Sorry for my English grammar mistakes.
This is not a programmatic solution but when you do automatic visual inspection it is the first thing you should try: Improve your set-up. The image is simply darker around the edges so increasing the brightness when recording the images should help.
If that's not an option you could consider having an empty image for comparison. What you are trying to do is background segmentation and there are better ways than simple color thresholding they do however usually require at least one image of the background or multiple images.
If you want a software only solution you should try an edge detector combined with morphological operators.

Find image inside another in SimpleCV

I'm using Python and SimpleCV (but is ok to use OpenCV too) and i have an image:
Futhermore, i have some small images, like this, which were cropped from the original image:
Assuming that the first image contains the second, I would like to get the second's image coordinates in regard of first, before cropping. How I can make this?
Use matchTemplate in OpenCV:
diff = cv2.matchTemplate(img1, img2, cv2.TM_CCORR_NORMED)
x, y = np.unravel_index(np.argmax(diff), diff.shape)

Categories

Resources