Crop excess white space from scanned TIFF - c#

I have documents that are being scanned that are less than 8.5 x 11. They are probably more like 8.5 x 5. The scanners being used are centering the documents vertically and still making 8.5 x 11 TIFFs. I'm looking for a way to automatically remove the white space from the top and bottom of the images. If the image's orientation is changed, there could potentially be excess white space on all 4 sides of the image. Assume there is no setting on the scanner to prevent it from doing this.
tl;dr
Does anyone know of a decent TIFF library that can auto crop images?

You don't necessarily need a "tiff library" to do this. you could do it in c#, something like this...
convert the tiff to a bitmap
inspect the bitmap, pixel by pixel to determine where the whitespace starts/ends
once you know the pixel coordinates of the surrounding whitespace, you can then crop the bitmap image
save the new image
Reference these pages for more information:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.croppedbitmap%28v=vs.85%29.aspx

Related

Determine boundaries of sheet music for cropping

I have thousands of images of sheet music, in 4:3 aspect ratio, that I want to crop to something closer to 16:9. The images have a lot of whitespace, so at least I want to cut that away.
All the images are different, even the margins aren't always the same.
What kind of algorithm should I use in C# to detect the edges of the staves, so I can determine where to crop the image?
(The green box is where I'd want to crop this image)
I've tried looping through all the pixels, that seems to work, but is extremely inefficient.
Is there a better way?

Create a way in .NET to determine X,Y locations on a PDF for printing

I have a PDF that is a template I will use to print onto special paper stock in a printer. I want to create a way to use the mouse to capture x,y locations when i click on certain areas of the pdf. The pdf template is based on an 8 1/2 x 11 piece of paper.
One of my ideas was to convert the page in the pdf to a jpg and load it into a picturebox using c# and a windows forms application. Then I can capture the x,y when i click the mouse but I'm not sure how that will translate to the pdf.
I'm open to opensource suggestions but I also have both the Aspose Words and Aspose PDF products. I'd also prefer a .NET solution.
Rather than storing a direct X/Y pixel co-ordinate then, you could store them as a % of the page.
So go down your jpeg idea, get the pixel X,Y of your location, then divide those by the Width,Height of the Jpeg to give you X%,Y%
When you come to do your printing, get the paper size and multiply that by your X%,Y% to get your offset within the print document
In the end what worked for me was finding out that 1 inch = 72 points. So as far as the Aspose library was concerned that conversion factor worked for me. THEN I created a jpeg that was 1 pixel by 1 pixel and would stamp that on the PDF using the XIndent and the YIndent. This allowed me to stamp a pixel in the upper left corner of pdf and then using the width and height of the page and the conversion factor I was able to deduce the other 3 corners!

How to check if bunch of jpg files are black with C#

So I have a program that scans cameras from multiple sources and takes a thumbnail of their view at a certain time and saves them as jpg's.
I would like to now scan these through my C# program and check if any of the created jpg files are completely black (either completely obstructed, or no signal in this case).
I am wondering what would be the best way of solving this problem. Not a color depth issue.
Thanks!
Use the GetPixel(x,y) function to check color at x,y location. You can iterate through the whole image and if they're all black then it's black. You can also check if majority of pixels are gray / black - if so then it's probably a very dim image.
Load picture.
Go through all pixels and check their RGB value.
If you find all below a certain threshhold - assume picture is black.
Beware: you should likely ignore single pixels not being black. Sensors are not perfect. Stuck pixels are a known phenomenon.

Image Modification (cropping and de-skewing) in C#

With a mobile device I take a picture of a flat light object on a dark surface. (for instance a coupon clipped out of a newspaper).
The image is then run through a brightness/contrast filter. If it is too dark, vital components are left out. If it is too bright, the writing on the coupon is lost.
This image is then converted into a bitonal image. Any pixel that is 50% or more dark is converted to black, everything else is white. (done)
I am left with a skewed bitonal image (think of a white trapezoid inside a larger rectangle with a black background).
I need to figure out how to crop the image - which when it's on a black background is easier than when it's on a white background. Then, I have to de-skew the image so it is rectangular instead of trapezoidal, while attempting to preserve aspect.
The end result should be a nicely cropped, bitonal, readable image of the coupon.
To crop your image, you can use the LockBits method and scan through all your pixels to find the first pixel with content from the top, left, right and bottom, respectively. How to use LockBits is described nicely here: https://web.archive.org/web/20141229164101/http://bobpowell.net/lockingbits.aspx
Assuming your image is not rotated, and that the skewing comes from the camera held at an angle against the table where the coupon is being photographed, you should now have a skewed image of the coupon, fitting perfectly within the bounds of the cropped bitmap. You should also know the four corners of the trapezoid.
"Undistorting" an image is not as easy as you might think though. However, good people have solved this problem and you can probably port their code to your own use. Here is a link I used to explore this problem in a similar case some time ago:
http://ryoushin.com/cmerighi/en-US/2007-10-29_61/Image_Distortion_Enhancements
I also have some code stored somewhere if you can't make any sense of what you find.

How can I resize an image in C# while retaining high quality?

I found an article on image processing from here: http://www.switchonthecode.com/tutorials/csharp-tutorial-image-editing-saving-cropping-and-resizing Everything works fine.
I want to keep the high quality when resizing the image. I think if I can increase the DPI value I can achieve this. Does anyone know if this is possible? And if so, how can I implement it in C#?
For starters, it's worth pointing out that there are two general categories of images; vector [e.g. SVG, WMF, Adobe Illustrator and Corel Draw Graphics] and bitmap (also called raster) images [e.g. Bitmap, JPEG and PNG Images].
Vector images are formed from a series of mathematical equations and/or calculations. Bitmap images, on the other hand, are made up of individual dots (pixels) each corresponding to a particular feature on the object the image is taken of.
If it should happen that you want to resize an image, the first thing to consider is if it is a bitmap or vector image. By virtue of the fact that vector images are obtained from calculations, they can be perfectly resized without losing any detail. The case is different for bitmap images. Since each pixel is independent of the other, when you desire to resize it, you are simply increasing or decreasing the source to target pixel ratio.
So in order to double the size of a vector image, simply multiply the target dimensions by two and everything comes out all right. If you should apply the same effect on a bitmap, you are actually increasing each source pixel to cover four pixels (two rows of two horizontal pixels).
Of course, by applying interpolation and filtering, the computer can "smooth" out the edges of the target pixels so they seem to blend into each other and give the appearance of a reasonably resized image but this output is never the same as resizing a vector image; vector images resize perfectly.
You also mentioned DPI in your question. DPI is essentially the number of pixels that correspond to an inch when the image is printed not when it is viewed on a screen. Therefore by increasing the DPI of the image, you do not increase the size of the image on the screen. You only increase the quality of print [which needless to say depends on the maximum resolution of the printer].
If you really desire to resize the image and the image is a bitmap, as a rule of thumb, do not increase the size beyond 200% of the original image's size else you'll lose the quality.
You can see this answer for code to resize bitmap images.
To see a sample vector image, go to this link.
Note Try zooming in and out of the image to see how well it resizes.
A typical bitmap are the StackOverflow sprites. They do not keep their quality resized.
Further Reading
Vector Graphics: http://en.wikipedia.org/wiki/Vector_image
Bitmap Graphics: http://en.wikipedia.org/wiki/Bitmap_image
Simply If the original image is smaller then the re-sized image then there is hardly anything you can do. Rest is a research problem.
This would only be possible if it's a vector graphic. Look into SVG. Otherwise, I think you might need Silverlight or Flex for that part.
What you're asking isn't really possible. You can't enlarge an image while maintaining the same quality.
If you think about an image as a mapped array of pixels (literally, a "bit-map"), this makes sense. The image is saved with a fixed amount of data, and that's all you have to work with when you resize it. Any examples to the contrary (like TV shows, as suggested by one of the comments) are purely fictional.
The best that you can do is set the InterpolationMode property of the Graphics object you're using to do the resizing to "HighQualityBicubic", which is the highest quality smoothing algorithm supported by GDI+ and in fact by every major graphics package on the market. It's the best that even Adobe Photoshop has to offer. Essentially, interpolation means that the computer is calculating the approximate value of the new pixels you're adding to make the image larger from the relative values of neighboring pixels. It's a "best guess" method, but it's the best compromise we've come up with yet.
At the very least, the resulting images won't have "jaggies" or rough, pixelated lines.
Sample code:
Graphics g;
g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// ... insert the rest of your code here
Beyond that, it's worth noting that GDI+ (which the .NET Framework uses internally for graphics routines) works best with image sizes that are multiples of 16. So if it all possible, you should try and make the width and height of your resized images a multiple of 16. This will preserve as much of the original image quality as possible.
The ideal solution is to switch to vector graphics that can be resized at will. Instead of pixel information, they contain mathematical information used to draw or "render" the image. Read more on Wikipedia.
let's try metadata in GDI+, may be it can suit your request

Categories

Resources