CNN: Why do we first resize the image to 256 and then center crop to 224? - pytorch

The transformation for Alexnet image input is below:
transforms.Resize(256),
transforms.CenterCrop(224),
Why do we first resize the image to 256 and then center crop to 224? I know that 224x224 is the default image size of ImageNet but why we can't directly resize the image to 224x224?

Perhaps this is best illustrated visually. Consider the following image (128x128px):
Say we would resize it to 16x16px directly, we'd end up with:
But if we'd resize it to 24x24px first,
and then crop it to 16x16px, it would look like this:
As you see, it's getting rid of the border, while retains details in the center. Note the differences side by side:
The same applies to 224px vs 256px, except this is at a larger resolution.

Related

I am trying to find the defects in following images. Not sure where to begin

`This has color defect.
This has crack defect.
This has scratch defect.
This has imprinting defect
input_img = cv2.resize(input_img,(500,500),interpolation=cv2.INTER_LINEAR)
gray_i_image = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY)
blur_image = cv2.blur(gray_i_image,(3,3))
`
This is what I know in which I am resizing the image, then converting it to grey scale and then clearing the noise from data image. After that I don't know what to do.
I want an output image in which the defected area is highlighted as in rectangle, I know we have to use contours for it. but I dont know how to.

Why is a generated SVG image less rich than the corresponding PNG image

To set this up, I used svgwrite library to create a sample SVG image (20 squares of length 100 at random locations on a display size of length 400)
import svgwrite
import random
random.seed(42)
dwg = svgwrite.Drawing('x.svg', size=(400,400))
dwg.add(dwg.rect(insert=(0,0), size=('100%', '100%'), fill='white')) # White background
for i in range(20):
coordinates = (random.randint(0,399), random.randint(0,399))
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
dwg.add(dwg.rect(coordinates, (100, 100),
stroke='black',
fill=svgwrite.rgb(*color),
stroke_width=1)
)
dwg.save()
I then wrote a sample pygame program to generate a PNG image of the same sample. (A seed has been used to generate the same sequence of squares.)
import pygame
import random
random.seed(42)
display = pygame.display.set_mode((400,400))
display.fill((255,255,255)) # White background
for i in range(20):
coordinates = (random.randint(0,399), random.randint(0,399))
color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
pygame.draw.rect(display, color, coordinates+(100,100), 0)
pygame.draw.rect(display, (0,0,0), coordinates+(100,100), 1) #For black border
pygame.image.save(display, "x.png")
These are the images that I got (SVG's can't be uploaded to SO, so I have provided a screenshot. Nevertheless, the programs above can be run to output the same).
My question is, why is the PNG (on the left) richer and sharper than the corresponding SVG image? The SVG looks blurred and bland, comparatively.
EDIT: One can notice the fine white line between the first two squares at the top-left corner. It's not very clear in the SVG.
Two things I think may impact:
You are using an image viewer, which could distort the vectorial SVG image. I think all of the vector images viewers get the actual screen size, then export the vectorial image into a matrix image sized in function of the size of the screen you have. Then they display the matrix image. If they render the image with softened sharpness, or if they have a problem by getting the size of your screen, the image may be blurred.
To make the PNG image, you use pygame. But you are using another module to make the SVG image. This module may function differently, and also exports the image with another quality than if you were exporting it with pygame.
For me personally the SVG image appears blurred with Gimp, for example, but not with another SVG viewer.
So I think the problem comes from your image viewer.

Xamarin: display image at real size

Trying to simply display an image at it's full size, which is bigger then the screen width and height. I do not care because I am developing a page for pinch and pan. So, the image will be off the screen anyways, and I would like to use a bigger image for pinch and pan, and it keeps setting the width to screen width when I just want it to show at real height and width. I've tried absolute layout, relative, and stack, nothing works. Here's my current code:
<controls:GestureFrame x:Name="GestureFrameMain">
<Image x:Name="ImageMain"/>
</controls:GestureFrame>
and in the backend, I simply set the source:
ImageMain.Source = Xamarin.Forms.ImageSource.FromUri(new System.Uri(PageParametersImageZoom.Image.BigImageURL));
I can verify that the URL has an image that is much larger than the screen size, here's an example of an image that would come back:
http://www.stage.newser.com/image/1088966-0-20161020082458.jpeg
And as you can see it's much larger than the dimensions of the screen, however, when I set the source, the image appears to be at screen size. I've attached an image of the result on the page, which clearly shows the image being downsized by Xamarin.
EDIT the pinch and pan itself is not the problem. When I pinch/pan the image obviously gets larger and goes off screen. I am talking about the initial load of the page, I would like the image to show at its real size.
Looks like the only way I was able to do this was by using a AbsoluteLayout and specifying the widhrequest/heightrequest properties on the image.

How can i force the imagemagick module of nodejs to output one single image only?

I am using the imagemagick module with Nodejs
im = require('imagemagick');
The imagemagick module uses the imagemagick command line tools.
I use the convert method to crop an image
im.convert([image_path, '-crop', '200x150', '-gravity', 'center', target_path],
function(err, stdout){}
);
This results in two images. The one with the cropped image area - the second with the image garbage i tried to get rid of.
How can i force imagemagick to output one image file only?
Per the imagemagick documentation for cropping, which is admittedly a little obtuse (emphasis added):
The width and height of the geometry argument give the size of the image that remains after cropping, and x and y in the offset (if present) gives the location of the top left corner of the cropped image with respect to the original image.
...
If the x and y offsets are present, a single image is generated, consisting of the pixels from the cropping region.
...
If the x and y offsets are omitted, a set of tiles of the specified geometry, covering the entire input image, is generated.
... so, you just need to specify your x and y offsets as part of your geometry argument, like so: 200x150-100-75
Notice that I've specified -100 and -75 for the upper left corner of your crop region, this is because you set your gravity to center, but it appears that imagemagick tries to intelligently determine the appropriate distance target based on your gravity, and I don't see exactly how it behaves when you choose center. So you may have to play around with this one a bit, or you could omit the gravity and use the actual offset from the top left corner of your original image.
I had to use the +delete parameter to remove the last image from the image sequence.
im.convert([image_file.path, '-crop', geometry, '+delete', thumb_path], ...

Reduction of dimensions in a JPEG image

I have to reduce the dimensions of a JPG image to exactly 350 * 450 pixels.Please tell how do I do so.My JPG image is of dimensions 392*520 pixels.
UPDATE:
Ok, thanks for the answers . Checking them now.
Thanks.
You can't do that with a linear operation, the ratio of the image will be skewed.
There is nearly 3 % absolute difference, I don't know how much that is to the human perception.
350/392 = 0.8929
450/520 = 0.8654
I would recommend scaling it to 89% and then cut the rest of the image to get your 350 * 450. This way you would keep the ratios intact.
You can use paint. Go to the resize button (on top of the rotate button) and click the radio box that says pixels. Choose the amount of pixels you want.
There are many online resize tools, e.g.: http://www.picresize.com/
Upload your image, enter its new dimensions and download it again.

Resources