How to calculate resolution based on megapixels and aspect ratio - c#

How can I calculate in C# the exact resolution based on megapixels?
Let's say I have a 5000x4000 pixels image and I want to rescale to 0.6mpx, how can I get the exact horizontal and vertical resolution to get an image of 0.6mpx keeping the original aspect ratio?
I have this:
int nXPixels=5000; // original image Width
int nYPixels=4000; // original image Height
float fRatio=5000/4000; // original image aspect ratio
int nNewPixelResolution=600000; // 0.6mpx new resolution
int nNewXPixels=?
int nNewYPixels=?

Amount of megapixels is equal to the product of the length and width of the resolution, divided by 1 million. So for your 5000x4000 pixels, you would have 20 Mpx. To rescale it to 0.6Mpx, but keeping the ratio, you would just need to rescale each dimension by the squareroot of 20Mpx/0.6Mpx, so that, when the newly computed dimensions will be multiplied, the result would be equal to 0.6Mpx
So your computation should be similar to :
ratio=sqrt(20/0.6)
newWidth=oldWidth/ratio
newHeight=oldHeight/ratio
If you then multiply newWidth with newHeight, the result should be aproximately 0.6Mpx, or 600000 px

int nXPixels=5000; // original image Width
int nYPixels=4000; // original image Height
//float fRatio=5000/4000; // original image aspect ratio
int nNewPixelResolution=600000; // 0.6mpx new resolution
int nOldPixelResolution=nXPixels*nYPixels;
double conv = Math.sqrt((double)nNewPixelResolution/(double)nOldPixelResolution);
int nNewXPixels= (int)Math.round(nXPixels*conv);
int nNewYPixels= (int)Math.round(nYPixels*conv);

Related

How to use setpixel for negative pixel value in c#?

When I am doing to get image from pixel by doing this and got error as:
Additional information:value of "-13" isn't valid.
So please help me.
bmp.Setpixel(x,y,Color.FromArgb(100,-12,100,100);
What to do for negative pixel value as above?
You should do one of two things.
Option 1 is what you appear to be doing now, but with a limit
int clampedRed = Math.Max(0, red - average);
// Repeat for Blue, Green
bmp.SetPixel(x,y,Color.FromArgb(100, clampedRed, ...)
Better however would be to not use the average pixel values, as that is going to drive half your image into black. Probably better to "normalise" the image. That means you need to find the MIN and MAX for each channel in the image (or a quartile) and then scale all pixels.
int minRed = // Get min in image
int maxRed = // get max in image
int rangeRed = maxRed - minRed
float scaling = 255 / rangeRed;
foreach (pixel in image){
int normalisedRed = (int)((pixelRed - minRed) * scaling)
int clampedRed = Math.Max(0, Math.Min(255, normalisedRed));
// And then use that...
}

Calculate how much to Pan/Tilt image

I am currently implementing Pan/Tilt/Zoom support in my application. I pass in an image and then calculate zoom in the following manner
int widthPercent = width / 100;
int heightPercent = height / 100;
int zoomX = width - (widthPercent * (int)Zoom);
int zoomY = height - (heightPercent * (int)Zoom);
where width is width of original image, height is height of original image and Zoom is a value passed in from the UI, ranging from 0 to 100.
I now wish to implement Pan/Tilt support while an image is zoomed in so that the whole image can still be accessed. Again Pan and Tilt will be controlled from the UI(again 0 to 100) but I want it to stay within the boundaries of the image so that the image does not repeat which it is currently doing.
I am current calculating the Pan and Tilt like so:
// Calculate Pan
int panWidthPercent = width / 100;
int finalPan = (int)Pan * panWidthPercent;
// Calculate Tilt
int tiltHeightPercent = height / 100;
int finalTilt = (int)Tilt * tiltHeightPercent;
This works to an extent however it seems to keep repeating the image after panning for a small time(usually when Pan = 10 or more). I wish it to stop after it renders all the image i.e it reaches the width but adding something like the following doesnt seem to stop this
if(finalPan >= width) finalPan = width;
Once the values are calculate I create a source rectangle using these values
mClippingRectangle = new Int32Rect(finalPan, finalTilt, zoomX, zoomY);
which is then used against the base image for rendering only that rectangle
In short how can I calculate how much I should pan/tilt when I already know the zoom of the image
Managed to accomplish it by checking whether the Pan/Tilt Amount added to the zoom width/height is greater than the overall image width/height then stop it from panning/tilting
// Calculate Pan
double finalPan = Pan * widthPercent;
if ((finalPan + zoomX) >= width)
{
finalPan = width - zoomX;
}
// Calculate Tilt
double finalTilt = Tilt * heightPercent;
if ((finalTilt + zoomY) >= height)
{
finalTilt = height - zoomY;
}

Inflate Rectangle by Percentage

I know to Inflate the Rectangle by Pixels, How to by Percentage?
Ex: rect.Inflate(45%,105%) the integer value should be passed as percent value not pixel value
How??
One option is to use the SizeF structure to allow for you to calculate the width and height values by percentage values, like this:
SizeF theSize = new SizeF(rect.Width * .45, rect.Height * 1.05);
SizeF holds floating point values, but unfortunately there is no overload for Inflate() that accepts a SizeF, but it does have an overload that accepts a Size structure. So we need to convert a SizeF to a Size, like this:
// Round the Size
Size roundedSize = Size.Round(theSize);
// Truncate the Size
Size truncatedSize = Size.Truncate(theSize);
Finally, we can use our converted Size (either the rounded or truncated), like this:
rect.Inflate(roundedSize);
OR
rect.Inflate(truncatedSize);
There is no function to do this. You will need to calculate the pixels from the percentage and use those instead.

Resize drawn rectangle to fit original image

I'm developing an application to manipulate images scanned on a wide-image scanner. These images are shown as a ImageBrush on a Canvas.
On this Canvas they can a make Rectangle with the mouse, to define an area to be cropped.
My problem here is to resize the Rectangle according to the original image size, so that it crops the exact area on the original image.
I've tried many things so far and it's just sqeezing my brain, to figure out the right solution.
I know that I need to get the percent that the original image is bigger than the image shown on the canvas.
The dimentions of the original image are:
h: 5606
w: 7677
And when I show the image, they are:
h: 1058,04
w: 1910
Which gives these numbers:
float percentWidth = ((originalWidth - resizedWidth) / originalWidth) * 100;
float percentHeight = ((originalHeight - resizedHeight) / originalHeight) * 100;
percentWidth = 75,12049
percentHeight = 81,12665
From here I can't figure how to resize the Rectangle correctly, to fit the original image.
My last approach was this:
int newRectWidth = (int)((originalWidth * percentWidth) / 100);
int newRectHeight = (int)((originalHeight * percentHeight) / 100);
int newRectX = (int)(rectX + ((rectX * percentWidth) / 100));
int newRectY = (int)(rectY + ((rectY * percentHeight) / 100));
Hopefully someone can lead me in the right direction, because i'm off track here and I can't see what i'm missing.
Solution
private System.Drawing.Rectangle FitRectangleToOriginal(
float resizedWidth,
float resizedHeight,
float originalWidth,
float originalHeight,
float rectWidth,
float rectHeight,
double rectX,
double rectY)
{
// Calculate the ratio between original and resized image
float ratioWidth = originalWidth / resizedWidth;
float ratioHeight = originalHeight / resizedHeight;
// create a new rectagle, by resizing the old values
// by the ratio calculated above
int newRectWidth = (int)(rectWidth * ratioWidth);
int newRectHeight = (int)(rectHeight * ratioHeight);
int newRectX = (int)(rectX * ratioWidth);
int newRectY = (int)(rectY * ratioHeight);
return new System.Drawing.Rectangle(newRectX, newRectY, newRectWidth, newRectHeight);
}
I think the only reliable option is to let your users zoom in to the image (100% or higher zoom level) and make a selection on part of the image. This way they can make an exact pixel-based selection. (Assuming that the purpose of your selection rectangle is to select part of an image.)
Your problem now is that you're using floating-point calculations because of the 75% zoom level and rounding errors will make your selection rectangles inaccurate. No matter what you do, when you try to make a selection on a shrinked image, you're not selecting exact pixels - you're selecting parts of pixels as you resize your rectangle. Since a partial pixel cannot be selected, the selection edges will be rounded up or down so you either select one pixel too many or one pixel too few in a given direction.
Another issue that I just noticed is that you distort your image - horizontally it's 75% zoom, vertically it's 81%. This makes it even harder for users because the image will be smoothed differently in the two directions. Horizontally 4 original pixels will be interpolated on 3 output pixels; vertically 5 original pixels will be interpolated on 4 output pixels.
You are actually doing a form of projection. Don't use percentages, just use the ratio between 5606 and 1058,4 = ~5.30. When the user drags the rectangle, reproject it which is selectedWidth * 5606/1058.4.

ImageResizer - that supports maxWidth & MaxHeight -> mean keeping aspect Ratio

I am looking for An ImageResizer Like below that supports MaxWidth & MaxHeight ...
where can i find it?
the below module does many other jobs that are not necessary for me.
just want to change format & support maxwidth and maxheight.
ImageResizer
You can write a wrapper that enforces the maximum width and maximum height, and maintains the aspect ratio.
For example, say you have an image that's 640 x 120 and your maximums are 1,920 x 1,440. Now, you want to make that image as large as possible, so you write:
ResizeImage(image, 1920, 1440)
If you were to do that, the aspect ratio would be shot.
You need to compute the aspect ratio of the existing image and adjust the values.
// Compute existing aspect ratio
double aspectRatio = (double)image.Width / image.Height;
// Clip the desired values to the maximums
desiredHeight = Math.Min(desiredHeight, MaxHeight);
desiredWidth = Math.Min(desiredWidth, MaxWidth);
// This is the aspect ratio if you used the desired values.
double newAspect = (double)desiredWidth / desiredHeight;
if (newAspect > aspectRatio)
{
// The new aspect ratio would make the image too tall.
// Need to adjust the height.
desiredHeight = (int)(desiredWidth / aspectRatio);
}
else if (newAspect < aspectRatio)
{
// The new aspect ratio would make the image too wide.
// Need to adjust the width.
desiredWidth = (int)(desiredHeight * aspectRatio);
}
// You can now resize the image using desiredWidth and desiredHeight
It doesn't matter if the library does more than what you need. If it does what you need it to, use it. The extra stuff isn't going to impair you at all.

Categories