Locate a resized subimage inside an image - c#

I am working in a project where each user has a big avatar and a thumbnail of this avatar. The avatar is 150x215 and the thumbnail is 50x50. To generate the thumbnail, the user selects a square area inside the avatar and the system crops and resizes the avatar to generate the thumbnail.
Now I need to have a 70x70 thumbnail. I cannot resize the 50x50 thumbnail because it does not look nice. My idea was to create a tool to find the thumbnail inside the avatar and, using the thumbnail location, generate the new 70x70 image. It was working well until I notice that some thumbnails are not only cropped, they are resized. When the image is resized it loses pixels what makes a pixel-by-pixel comparison impossible (so I can't detect the thumbnail location inside the avatar).
Is there any way to identify where the thumbnail is located(even though it is resized)? I am using EMGU to handle the images.
Thanks for any help

[EDIT1]
Seeing your note, if you had the scaling factors applied to the original avatar, then you could create a temp thumbnail that has the same scaling factor applied, then perform a statistical equivalence check of the thumbnail against the already scaled avatar. What this would look like is finding the "difference image" of the thumbnail against the scaled avatar image for each location the thumbnail can possibly lie within the avatar. for each of these "difference images" add all of the pixel based differences into a combined single numeric difference and store that into a 2D array sized to the dimension of x and y locations the thumbnail can possibly be placed within the scaled avatar image (this will be smaller than the total avatar image size, infact it will be width = avatarWidth - thumbWidth and height = avatarHeight - thumbHeight). After you have calculated all of the single difference instances for this 2D array, simply find the min within the array and that is the top-left pixel location within the scaled avatar to use. You will of course have to take the new scaled size of the 50 x 50 thumb into account when grabbing your 70 x 70 from this calculated top-left point.
You dont (by the way) have to store this 2D array of difference values, You could simply hold a min location that is initialized with the value from the first tested location, and only update if the current location is less than the current min. This would avoid the added storage of the array.
[ORIGINAL]
Once the avatar image has been resized, it has also been interpolated, which for all intensive purposes means that the original pixel information has been mathmatically changed irreversibly.
You may have better luck getting into the original thumbnailing code, and changing the thumbnail code to grab the 70 x 70 px sub-image, then create the 50 x 50 by cropping another 10 px from each side!!! This is assuming you still need both the 50 x50 and the 70 x 70 thumbnails.

Related

Image size in DB?

I made an application that stores Item picture (jpeg) on SQL Server DB, I am using pictures shot with my camera (file size ~ 2 M). I am checking if picture.Width > 800 or picture.Height > 600, then I resize the picture to 800x600.
If I export picture from DB, the file size is about 100k, and if I open the same picture in photoshop, the image size is shown 1.37M.
Now my questions are:
First, I want to know the space that this picture takes in my DB.
The reason I'm resizing the picture before storing it in my DB, is that I imagine it taking a huge space in my DB.
Second, How do I resize a picture to keep it's aspect ratio?
The size of the jpg file depends of the widht, height and the color dept. Check this link http://jan.ucc.nau.edu/lrm22/pixels2bytes/calculator.htm to calculate the image size.
to resize the picture size and keep the aspect ratio there is a lot of libraries and framewrok to complete the task. I do not recomend you to implement one unless
you have to (there is a lot of good libraries). check this post Image resizing algorithm.
Its not good idea to store image in DB check Storing Images in DB - Yea or Nay?

How to split/slice image give each image Id to save in Database C#

I'm tying to figure out away to split/slice a large image >5000x5000 PX into small images using a grid system where each slice have its unique ID then save images in database
Each slice should have its location (width,height + X,Y) on the original image (maybe in array) so later on i can ReBuild the original image using these slices
http://i.stack.imgur.com/PKYem.jpg
You can use Bitmap.Clone Method (Rectangle, PixelFormat) to slice the image rectangle by rectangle.

Image Co-Ordinate System Design

I'm diving into something without sufficient background, but I feel like there may be simple solutions that don't require me to have in depth knowledge of the topic.
What I am trying to do is have an image co-ordinate system. Basically the user will supply an image, like a house plan. They can then click on points in the image and create markers (like google maps). The next time they retrieve the map, all the markers they added before are there and they can add new ones.
I need to identify the points these markers are located on so I can store that information. I also need to be able to create a layer on the image that contains the markers and renders them in the exact locations they were placed.
I imagine the easiest way to do this is to use pixel co-ordinates...the rub here is that the image won't be a fixed size since there is a web application and an IPad application, so the co-ordinate system needs to work as long as the image is in the same size ratio.
The server size is .NET and as mentioned there is an IPad app, so the solution needs to be viable given that tech stack.
Any ideas?
Instead of using pixel coordinates in absolute terms, you can use the 0 to 1 range. The top left corner is (0,0), bottom right is (1,1) and the center of the image is (0.5,0.5). This way not matter what image size (or zoom level) you have, the markers will always be in the same place.
My suggestion is don't try to figure out the correlation between the actual image and the coordinates. The only thing I would do is use the resolution of the image, aka 800x600 and use that for your grid. Then overlay your markers using that grid on the image. The points you'd remember would just be X and Y values and maybe a tag name/id.

imageList.Draw doesn't draw with different size?

The ImageList has a method named "Draw":
imageList.Draw(graphics, bounds.X, bounds.Y, bounds.Width, bounds.Height, imgIndex);
I use this method to draw an image on a graphics object of a PrintDocument. When using the original image size (16 x 16 pixels), the image is drawn correct. If however, I change the bounds size, nothing is drawn. Even changing the size to 32 x 32 (double size) has no effect. Nothing is drawn. I need to change the drawn size because of the different dpi ... Where am I gong wrong ?
Edit: The solution seems to be simply to use the g.DrawImage method instead. Why imageList.Draw() doesn't draw is still a mistery to me ...
g.DrawImage(imageList.Images[imgIndex], bounds);
ImageList.Draw() is a bit unusual, it takes advantage of the built-in support that the native image list code inside of Windows has for rendering an image in the list. This is an optimization, it avoids the cost of converting the internal image as stored in the native image list back to a managed Image object.
One side-effect however is that this drawing happens without regard for any of the transforms that were applied to the Graphics object. A 16x16 image in the list is going to be rendered as 16x16 pixels on paper. Which is indeed a bit hard to find back, printers have very high resolution (600 dots per inch is typical), that image turns into a decimal point.
Image lists were really meant to be the source of images for the TreeView and ListView controls, it is not a good general purpose collection object for images. Like a List<Image>. Your workaround is good, the Image property converts the internal bitmap back to a managed Image, Graphics.DrawImage() will then scale it appropriately to get a size on paper that's close to the size on the screen. However with the graininess you get from making an image 6 times larger. Note that you should Dispose() that object.

Setting image DPI in relation to height/width C#

I'm writing an application to send some images to a third party, and the images must be 200x200 DPI. The image is a Bitmap and is sized at 500 width and 250 height.
The first time I tested the images with the third party, my resolution was incorrect. I merely used image.SetResolution(200,200) to correctly set it to 200x200. This, however, only changed the resolution tag for the image and did not properly, according to my third party technical contact, adjust the image height and width.
Is there a ratio that I can use so that for each X units I increment the resolution, I merely increment the corresponding height or width Y units? I thought that I could just increment resolution without having to increment height or width.
Thank you,
Aaron.
An image stored digitally has no meaningful concept of DPI. DPI comes into play when reproducing an image on a physical device.
You need to adjust the image size with regard to the DPI of the physical device, and the desired size of the output on that device.
For example, if a printer tells you they need an image at 300dpi to fill a space of 4in x 4in then you would provide them a bitmap with a size of 1200x1200 pixels. This image would end up with a physical size of 4in x 4in on a 300dpi output device. On a 600dpi device the same image would have an output size of 2in x 2in.
When dealing with digital images, you usually refer to PPI, which is pixels per inch. DPI is not directly related to digital image resolution.
So, if you look at a image that is 200px by 200px # 200PPI, you will have an image that is 1 inch by 1 inch.

Categories