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.
Related
We are developing an app where we need to crop an image according to the selecting object area. User will draw a line and we need to select the object and crop it .This crop need to be like the app: YourMoji
So far we have tried to get the color of the pixels along the line and then comparing those with the color of every pixel in the image and making a path from it to clip the image. But the almost going no where.
Is it possible through this way to crop an image or we are going in the wrong way? Can anyone provide a way to do this Or suggest a way to modify the way we have worked so far? Any advice and suggestions will be greatly appreciated!
Thanks in advance.
I guess what you want is the image segmentation algorithm called Graph Cut.
Here are two Github repositories, hope these would help:
GraphCut
GrabCutIOS
I'm not exactly clued up on image manipulation, but the first algorithm that comes to mind is something like this:
Take the average of the pixels in the line (as you have)
Since you appear to want faces, you might want to weight reds and blues over green. Not much green in faces of any skin tone.
For each pixel, if the colour is within a given threshold outside of your selected average, remove it / make transparent.
Perhaps the closer to the original line (or centroid), the less strict the threshold becomes.
I'd then provide the user with some tools for:
Sensitivity: how large the threshold is
Eraser: to remove parts of the image that your algorithm missed
Paintbrush: to replace parts of the image that your algorithm incorrectly removed.
I have an image I am reading from a pdf file and converting to jpg. It works fine, until I apply "resize_to_fit" - which results in a black-rectangle (of the specified size).
file = file + "[0]"
jpg_file = file + ".jpg"
pdf = Magick::Image.read(file) do
self.quality = 80
self.density = '300'
self.colorspace = Magick::RGBColorspace
self.interlace = Magick::NoInterlace
end
pdf.first.resize_to_fit!("600")
}
pdf.first.write(jpg_file)
Subsituting:
pdf.first.change_geometry!('600x600') { |cols, rows, img|
img.resize!(cols, rows)
}
... for the resize makes no difference, nor changing the quality, or the density, nor omitting the colorspace and interlace settings.
Since I have a good image at full size (a mostly white image), I don't see why "resize" or "change_geometry" would output pure black.
Ideas?
Dozens of random experiments later, I found the only conversion of size which does not output a black rectangle, is:
pdf.first.sample!(0.25)
The limitation, of course, is that I must have a consistent input-size for this to work, as the other argument-set (x and y) will change the aspect-ratio.
Also, the quality produced by 'sample' is horrible, no matter the settings applied on the input or output side.
I need a way to get resize_to_fit to work properly. I am following the docs and examples, so the result makes no sense. I really hope someone who uses rmagick often, and is familiar with what parts of it are not broken, or what I am doing wrong, can respond with help. Thanks
Answer from #bumpy was the solution. I am now doing it a different way using Carrierwave, but I rewound the code and did an A:B test; the line
pdf.first.alpha(Magick::DeactivateAlphaChannel)
... works. Note that Carrierwave does the conversion correctly, and with decent quality results (identical to this solution), without any special settings. I would guess this is built into its defaults for conversions to jpg.
It's possible your PDF file has a transparent background, which is causing the problem. Try removing the alpha channel before the resize using
pdf.first.alpha(Magick::DeactivateAlphaChannel)
pdf.first.resize_to_fit!("600")
I'm trying to get a bounding box for the word "ЛИЛИЯ" in this image, using opencv.
(source: litprom.ru)
I am already experimenting with cv::findContours() and different thresholding alogrithms for couple of days, but can not get any satisfying results.
So, what do I know about this word:
letters are of similar size;
letters' height is in range: 40px — 90px;
word is oriented horizontaly (±5˚);
there is one and only one word on this image;
this word does not intersect image's border (it's fully visible);
different parts of image may have different luminosity;
hotspots (totally white areas) may be present on an image.
English is not my native language, so I'm sorry if the question is not properly explained.
If someone needs more images to answer this question, I have at least a dozen more.
Check out stroke width transform. That is used to text detection.
You can preprocess your image with adaptiveThreshold. You should use a blocksize a little bit bigger than your biggest character. I tried on your image with 91 and it gave good results. Then you can use FindContours and filter the blobs/contours using their height. Note that the letters will still be connected one to another so you cannot really filter using the width.
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
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.