My usual method of 100% contrast and some brightness adjusting to tweak the cutoff point usually works reasonably well to clean up photos of small sub-circuits or equations for posting on E&R.SE, however sometimes it's not quite that great, like with this image:
What other methods besides contrast (or instead of) can I use to give me a more consistent output?
I'm expecting a fairly general answer, but I'll probably implement it in a script (that I can just dump files into) using ImageMagick and/or PIL (Python) so if you have anything specific to them it would be welcome.
Ideally a better source image would be nice, but I occasionally use this on other folk's images to add some polish.
The first step is to equalize the illumination differences in the image while taking into account the white balance issues. The theory here is that the brightest part of the image within a limited area represents white. By blurring the image beforehand we eliminate the influence of noise in the image.
from PIL import Image
from PIL import ImageFilter
im = Image.open(r'c:\temp\temp.png')
white = im.filter(ImageFilter.BLUR).filter(ImageFilter.MaxFilter(15))
The next step is to create a grey-scale image from the RGB input. By scaling to the white point we correct for white balance issues. By taking the max of R,G,B we de-emphasize any color that isn't a pure grey such as the blue lines of the grid. The first line of code presented here is a dummy, to create an image of the correct size and format.
grey = im.convert('L')
width,height = im.size
impix = im.load()
whitepix = white.load()
greypix = grey.load()
for y in range(height):
for x in range(width):
greypix[x,y] = min(255, max(255 * impix[x,y][0] / whitepix[x,y][0], 255 * impix[x,y][1] / whitepix[x,y][1], 255 * impix[x,y][2] / whitepix[x,y][2]))
The result of these operations is an image that has mostly consistent values and can be converted to black and white via a simple threshold.
Edit: It's nice to see a little competition. nikie has proposed a very similar approach, using subtraction instead of scaling to remove the variations in the white level. My method increases the contrast in the regions with poor lighting, and nikie's method does not - which method you prefer will depend on whether there is information in the poorly lighted areas which you wish to retain.
My attempt to recreate this approach resulted in this:
for y in range(height):
for x in range(width):
greypix[x,y] = min(255, max(255 + impix[x,y][0] - whitepix[x,y][0], 255 + impix[x,y][1] - whitepix[x,y][1], 255 + impix[x,y][2] - whitepix[x,y][2]))
I'm working on a combination of techniques to deliver an even better result, but it's not quite ready yet.
One common way to remove the different background illumination is to calculate a "white image" from the image, by opening the image.
In this sample Octave code, I've used the blue channel of the image, because the lines in the background are least prominent in this channel (EDITED: using a circular structuring element produces less visual artifacts than a simple box):
src = imread('lines.png');
blue = src(:,:,3);
mask = fspecial("disk",10);
opened = imerode(imdilate(blue,mask),mask);
Result:
Then subtract this from the source image:
background_subtracted = opened-blue;
(contrast enhanced version)
Finally, I'd just binarize the image with a fixed threshold:
binary = background_subtracted < 35;
How about detecting edges? That should pick up the line drawings.
Here's the result of Sobel edge detection on your image:
If you then threshold the image (using either an empirically determined threshold or the Ohtsu method), you can clean up the image using morphological operations (e.g. dilation and erosion). That will help you get rid of broken/double lines.
As Lambert pointed out, you can pre-process the image using the blue channel to get rid of the grid lines if you don't want them in your result.
You will also get better results if you light the page evenly before you image it (or just use a scanner) cause then you don't have to worry about global vs. local thresholding as much.
Related
I have one picture.
There are many broken places in the image.
Please refer to the the picture.
Who knows how to repair the broken stroke using opencv 3.0?
I used dilate operation in OpenCV and I got the picture as belows:
It looks so ugly if comparing the original image.
I am late to the party but I hope this helps someone.
Since you have not provided the original image I cannot say the following solution would work 100%. Not sure how you are thresholding the image but adaptive thresholding might give you better results. Opencv (Python) code:
gauss_win_size = 5
gauss_sigma = 3
th_window_size = 15
th_offset = 2
img_blur = cv2.GaussianBlur(image,(gauss_win_size,gauss_win_size),gauss_sigma)
th = cv2.adaptiveThreshold(img_blur,255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY_INV,th_window_size,th_offset)
Tinker around with the parameter values to see what values work best. It's usually a good idea to blur your image and that might possibly take care of broken binary images of alphabets. Note, blurring may eventually produce slightly thicker characters in the binary image. If this still leaves with a few broken characters then you can use morphological closing:
selem_shape = cv2.MORPH_RECT
selem_size = (3, 3)
selem = cv2.getStructuringElement(selem_shape, selem_size)
th = cv2.morphologyEx(image, cv2.MORPH_CLOSE, selem)
Again, tinker around with structuring element size and shape that works best with your images.
I want to find circles in an image by using cvHoughCircles() .
But I confused about the fourth parameter, because when I use "1", the cvHoughCircles() does not find circles and when I use "2", the method work properly and detect all circles in the image.
Click Here to see the screenshot of my program for both cases.
I did the same operation on another image ,but this time changing the value of the fourth parameter from 1 to 2, didn't affect the result[cvHoughCircles() returned the same result for both cases( using 1 or 2 for the value of the fourth parameter)] .
Can anyone please tell me what value should be sued for the fourth parameter when working with different images?
Check out this link:
http://docs.opencv.org/modules/imgproc/doc/feature_detection.html
It lists the c/c++/python implementations for all the functions stating what each parameter does and i've always found that one of them is what javacv has been wrapping (in this case the c code). I was actually looking for this page when i came across your post so in case it happens again i can now follow my own link (awesome!). Now to answer your question as best I can.
The function looks like this:
CvSeq* cvHoughCircles(CvArr* image, void* circle_storage, int method, double dp, double min_dist, double param1=100, double param2=100, int min_radius=0, int max_radius=0 )
Where the site describes:
dp – Inverse ratio of the accumulator resolution to the image resolution. For example, if dp=1 , the accumulator has the same resolution as the input image. If dp=2 , the accumulator has half as big width and height.
I am guessing (based on what i remember from class) that what this refers to is the pyramid scheme that is sometimes used in feature detection. Basically you average the pixels of an image to get a smaller image in order to find the locations of important features like corners or in this case circles which in the end is based on gradient information (hence the black and white or greyscale image that should be used).
Using dp=1 should be perfectly fine however, just make sure to call cvSmooth() on the image so the gradient vectors make a nice circle around the circle. If you know that there is a circle then you could keep smoothing and dilating (cvDilate) until the circle appears but then you may detect artifacts so the biggest circle should be of what's interest. In the end it depends on the situation you are putting the algorithms through.
Does anyone know how to adjust the brightness of an image using RMagick? Rmagick has a number of different functions available, including ones to adjust levels and the hue/brightness/saturation levels, but I need to adjust the old-fashioned brightness/contrast levels.
There are custom functions for me to individually adjust each color channel (RGBA), but I'm not sure how to use levels to adjust the overall brightness. Messing with the different channels has yielded images that are color-altered. On GIMP, in the levels menu, the desired functionality I want is under 'Output Levels'. By dragging this below 255, I can achieve a 'darkening' effect. Is there some kind of equivalent in RMagick to control Output Levels? I don't see a channel for it.
Examples:
THIS IS ORIGINAL IMAGE:
THIS IS WHAT I WANT:
THIS IS WHAT HAPPENS WHEN I ADJUST LIGHTNESS (Rmagick's Modulate)
I think this should do what you need.
img = Magick::Image.read('bT9xc.png')
img.first.level(-Magick::QuantumRange * 0.25, Magick::QuantumRange * 1.25, 1.0).write('out.png')
This sets the black point and the white point 'further away' from the range found in the image, which has the effect of making the brightest white in the source image darker, and the darkest black in the source image lighter.
If you want to make it darker overall, just increase the second factor to Magick::QuantumRange * 1.5 or higher.
I think you can use the modulate method: http://www.imagemagick.org/RMagick/doc/image2.html#modulate
So to increase the brightness by 50% it would be something like:
img.modulate(1.5)
I'm using the Emgu shape detection example application to detect rectangles on a given image. The dimensions of the resized image appear to impact the number of shapes detected even though the aspect ratio remains the same. Here's what I mean:
Using (400,400), actual img size == 342,400
Using (520,520), actual img size == 445,520
Why is this so? And how can the optimal value be determined?
Thanks
I replied to your post on EMGU but figured you haven't checked back but this is it. The shape detection works on the principle of thresh-holding unlikely matches, this prevents lots of false classifications. This is true for many image processing algorithms. Basically there are no perfect setting and a designer must select the most appropriate settings to produce the most desirable results. I.E. match the most objects without saying there's more than there actually is.
You will need to adjust each variable individually to see what kind of results you get. Start of with the edge detection.
Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold, cannyThresholdLinking);
Have a look at your smaller image see what the difference is between the rectangles detected and the one that isn't. You could be missing and edge or a corner which is why it's not classified. If you are adjust cannyThreshold and observe the results, if good then keep it :) if bad :( go back to the original value. Once satisfied adjust cannyThresholdLinking and observe.
You will keep repeating this until you get a preferred image the advantage here is that you have 3 items to compare you will continue until the item that's not being recognised matches the other two.
If they are the similar, likely as it is a black and white image you'll need to go onto the Hough lines detection.
LineSegment2D[] lines = cannyEdges.HoughLinesBinary(
1, //Distance resolution in pixel-related units
Math.PI / 45.0, //Angle resolution measured in radians.
20, //threshold
30, //min Line width
10 //gap between lines
)[0]; //Get the lines from the first channel
Use the same method of adjusting one value at a time and observing the output you will hopefully find the settings you need. Never jump in with both feet and change all the values as you will never know if your improving the accuracy or not. Finally if all else fails look at the section that inspects the Hough results for a rectangle
if (angle < 80 || angle > 100)
{
isRectangle = false;
break;
}
Less variables to change as hough should do all the work for you. but still it could all work out here.
I'm sorry that there is no straight forward answer, but I hope you keep at it and solve the problem. Else you could always resize the image each time.
Cheers
Chris
I'm lost and in need of direction.
We're trying to render a bunch of small images (X) onto a single, unified canvas using imagemagick.
The different X's can be one of five different sizes: 20x20, 40x40, 60x60, 80x80 or 100x100 each. The large image's width is always set to 600, but the height can be regulated as needed.
We can be using as few as 10 or as many as 10,000 X's at any given moment.
Currently, the bare-bones proof of concept we're working with goes something like:
images.each do |image|
image = Magick::Image.read("#{RAILS_ROOT}/public/images/#{image}").first
w = image.columns
h = image.rows
pixels = image.export_pixels(0, 0, w, h, "RGB")
img.import_pixels(x, y, w, h, "RGB", pixels)
x += w
end
...it's simple and stupid, but it does output a series of images merged into one. Almost there ;-)
Does anyone know of an effective algorithm with which we can iterate many X's and place them side-by-side, spanning multiple lines and still optimizing the space? The goal here is to create a single image without white space, constructed by all small images.
As stated, I would love any feedback you guys might have on this. Pointers? Ideas? Examples?
Thanks.
It seems like right now the images are noise. You want to solve a tiles problem. The tiles have some fixed size and you want to put them on a surface of a fixed width and minimum height. This can be done globally with DFS, BFS, A*, etc. You can also look at some local method like simulated annealing or hill climbing, depending if you need to global optimum or just a good, reasonable solution. You can find implementations of these methods in the online source repository for AIMA.
Once you have solved the tile problem you can overlay the images, with as piece of code similar to the one you're showing.