What happens to pixels when an image is resized? - python

I have this original image of size 800 x 600px
What I have to do is to resize the image to 625 x 480px and filter all the land areas. I have found that the BGR values of the land part is (95,155,212). This is the code I used to filter all the and areas:
image[np.where((image == [95,155,212]).all(axis = 2))] = [0,0,0]
If I resize first, then filter, here is the output:
If I filter first then resize, I get my desired output:
So my first question is what happened to the image's pixels when it is resized?
I have this original image of size 712 x 480px
When I applied filtering to remove the land area, I get an output like the second image from the top. 2nd question, is there any way for me to fix this problem?

most likely the resizing changes the border colors to something between land color and black outline.
This screws up your filter because you need higher ranges for land color and also the border line color (Black) can have color artifacts. These artifact are what is left after filtering in your example. If you pick their colors they should be outside your selected range.
How to repair?
use nearest neighbor resizing
this will left the colors as are but the resized image is not as pretty ...
change filters to handle close colors not just range of color
so change to something like flood fill and fill all pixels that do not differ too much from each other. You need 2 thresholds for this:
absolute (is the color range total big one)
relative (is the max change of neighboring pixels small one)
now just recolor the resized image or change the filter function to this ...

Image sizes onscreen and in print
The size of an image when you view it onscreen is different from its size when you print it. If you understand these differences, you can develop a better understanding of which settings to change when you resize an image.
Screen size
The screen resolution of your monitor is the number of pixels it can display. For example, a monitor with a screen resolution of 640 x 480 pixels displays 640 pixels for the width and 480 pixels for the height. There are several different screen resolutions you can use, and the physical size of the monitor screen usually determines the resolutions available. For example, large monitors typically display higher resolutions than small monitors because they have more pixels.
Image size onscreen
Images are of a fixed pixel size when they appear on your monitor. Your screen resolution determines how large the image appears onscreen. A monitor set to 640 x 480 pixels displays fewer pixels than a monitor displaying 1024 x 768 pixels. Therefore, each of the pixel on the 640 x 480 pixel monitor is larger than each pixel displayed on the 1024 x 768 pixel monitor.
A 100 x 100-pixel image uses about one-sixth of the screen at 640 x 480, but it takes up only about one-tenth of the screen at 1024 x 768. Therefore, the image looks smaller at 1024 x 768 pixels than at 640 x 480 pixels
The Following Parameters change when you resize an image
Pixel dimensions: The width and height of the image.
Image size :
Document size: Physical size of the image when printed, including a width and height.
Image resolution when printed: This value appears in pixels per inch or pixels per centimeter.
In Photoshop the physical size, resolution, and pixel dimensions of an image are calculated as follows:
Physical size = resolution x pixel dimensions
Resolution = physical size / pixel dimensions
Pixel dimensions = physical size / resolution
For more info on this you can check Adobe's Document on Image resizing

Related

Moviepy: Resize image with black borders

I have a problem with resizing videos in moviepy. When I use the clip.resize(1920,1080) function, it stretches the image. I want it to stretch the image, keep the aspect ratio, but add black borders to it (to fill the aspect ratio).
This is the source image/video (resolution: 670 x 844)
This is how I want it to look (resolution: 1920 x 1080)
Mix it with some black image in resolution you whish:
video = mp.VideoFileClip("/media/pi/video.mp4")
black_image = (mp.ImageClip("black_picture_1920x1080.jpg"))
mp.CompositeVideoClip([black_image, video.set_position("center")])

Rescaling the bounding box annotations in a stitched image

I have two images stitched together just as shown below:
Each image is of resolution 1024 x 1024. This infers the total dimension on the stitched image is 2048 x 1024. The bounding box annotations on the images are given by:
For the first sub-part of the stitched image, I can directly use the annotations. For the second part of the stitched image that spans from 1025-2048 pixels in the 'X'-axis direction, I have to rescale the coordinates so that I get the annotations in the 1-1024 pixel regime. How should I rescale/modify the second part of the annotation to get the pixel values in the X direction to get them in the 1-1024 pixel regime?
If your stitched image is always a horizontal stack, then you can subtract the width of the first image from the x-position of the bounding box. In this example, if you see a bounding box that has an x-pos greater than 1024 you can assume it's on the right side picture so you can subtract 1024 to put it back in the [1, 1024] range.

What does keras.preprocessing.image.load_img do during resizing?

What does keras.preprocessing.image.load_img do during image resizing?
In the following statement, target size is set to 128x128.
image1 = img_to_array(image.load_img(img, target_size=(128,128))) / 255
What if I load 100x100 size image?
Will it leave the image as it is or will it zoom to 128x128 size?
It will actually resize it to the target size!
If the image size is smaller than the target size it will be stretched to fit the desired size.

How can I change the background of only white pixels?

I have two grayscale images of the same size, one of them is this one:
I'm trying to add a background to this image, which is to just change the white pixels to the respective pixels in the other picture. The best result I've managed to do is just a bitwise and of all the pixels of both pictures but the resultant picture is distorted inside James Bond. I also tried a weighted add between the two pictures but when I increase the weight of the James Bond image, it's white pixels are visible in the resultant image.
To combine with a second image, ensure that both images have the same dimensions (which yours do). They can then be combined
import cv2
img_jb = cv2.imread('james_bond.png')
img_007 = cv2.imread('007_logo.png')
height, width, channels = img_jb.shape
img_007_resized = cv2.resize(img_007, (width, height), interpolation=cv2.INTER_CUBIC)
threshold = img_jb > 240
img_jb[threshold] = img_007_resized[threshold]
cv2.imwrite('james_bond_logo.png', img_jb)
Giving you:
numpy allows you to work on the indexes of an array that match a given criteria. This has the effect of copying pixels from the background image into the foreground image where the foreground image has a value above 240.

OpenCV Python: Window Size and Mouse Events Coordinates

I'm trying to use mouse events and store the coordinates over which the cursor browsed. The problem is that my images are very small, 96x96 pixels and OpenCV chooses a window size that has bigger width than my images. So my image only takes the left side of the window. But the coordinates that are recognized by OpenCV correspond to the window size, so if I move the cursor to the middle of the window, only then are coordinates on the image itself marked at the middle. E.g. in this image the cursor was placed on the middle of the window and not the image:
I tried using the WindowResize function, but for some reason it does not work with images of such a small size, I'm assuming that this is the smallest window size in OpenCV.
Does anybody have any idea of how to make the mouse coordinates actually correspond to the coordinates in the image itself and not the window, or how to make the window size correspond exactly to the size of the image with very small images (96x96)?
I think it can be done by Scaling up your image size.
Here is some python code.
scaleFactor = 10
rows, cols = img.shape[:2]
img = cv2.resize(img, (scaleFactor*cols, scaleFactor*rows), interpolation=cv2.INTER_LINEAR)
Then get mouse position and scale down. ( pseudo code...)
px, py = getMouseClickPosition()
px /= scaleFactor
py /= scaleFactor

Categories

Resources