Now I am having a code which counts the height and width of a paragraph and sets it accordingly. However I have been having this strange problems whenever a break line(\n) passes through my paragraph I use this code to calculate my Height. I also calculate the width and make sure a line is properly fit.
float textSize = t.getTextSize();
Paint paint = new Paint();
paint.setTextSize(textSize);
However for some reason a break line couldn't have the height calculated which would mean me missing a few lines or show me half a line cause of the break lines during my performed calculations.
My question is, how would I undergo the calculation of the height of a break line of the space it occupies?
I wasn't able to solve this issue. However I did try to just delete 3 more extra characters at the edge of the end point of the width. It worked. However the real problem lies more in the character width. If a character is not registered with android a calculation vs the actual out come can be very different if you have letters that are completely different that off the regular alphabet.
Using this code you can determine the edge of the endpoint.
totalCurrentWidth = t.getPaint().measureText(s.substring(start, end));
However characters not registered in the system may have a different end or no end at all(Chinese or taiwan for example).
During each individual characters used in verdana it produces a different spacing compared to the actual outcome of the text.
If anyone find something wrong with my logic feel free to comment me. I only strive to improve after all.
Related
Already using HoughLinesP for detecting lines in the image, but the issue is it detects only straight, clean lines and misses out rest
here's the code
int threshold = 80;
double minLineSize = 100;
double lineGap = 10;
double theta=Math.PI / 180;
Imgproc.HoughLinesP(edges, lines, 1, theta, threshold, minLineSize, lineGap);
when it is applied on the below image
https://i.stack.imgur.com/2HFFY.png
The lines identified can be seen as below
https://i.stack.imgur.com/loc4k.png
As you can see the actual lines(Document boundary) I wanted to detect are being skipped.
Steps I followed
Changed to grayscale
Applied canny edge detection
findContour
If contour with appropriate area is not found, trying to find large lines & build the boundary manually
Issue i'm facing is that the houghlinesP is not detecting required lines and hence I'm unable to design my document boundary
Please suggest any other method to detect lines, and since I will use this code as a generalized one, I will not be able to modify the threshold & other values once I have configured.
I have also referred many other similar questions in stackoverflow but most of them suggest to use HoughLines but with different parameter values.
One of the question I found similar to my issue, but no answer.
Detecting incomplete rectangles (missing corners/ short endges) in OpenCV
Dark White lines are the ones detecting by HoughLinesP method
Also find below other sample images where the lines are being detected wrongly
https://i.stack.imgur.com/fWjti.png
https://i.stack.imgur.com/1vaut.png
https://i.stack.imgur.com/KXosQ.png
I'm curious why there is no implementation for Sheet.setColumnWidth that accepts a Points parameter.
There is a method to set the height of a Row in points, which is setHeightInPoints
The only available method to set the width is accepting a rather weird scale, defined as
width = Truncate([{Number of Visible Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
This means it depends on some width of a digit, so the value used in the parameter is
just 1/256 of what is wanted here, some straight representation of a width. In Excel itself
you set the width in Points, too.
This means behaviour of Row-height and Column-width is not symetric.
Is there any rational reason for only having that version of setColumnWidth?
This leads to serious problems, as the best I can get has a different result on every computer,
because the width which is set is depending on the users default font setting.
I believe it has something to do with displaying nicely on many different setups, as the comment of pnuts suggests. But it is only usable in a very narrow field.
At the moment I believe that there is no simple workaround for that, and I cannot find one right now. (just one that works for my case perhaps)
Is there any good way to calculate a column width value from a desired points value?
I.e. I want 120 points on any computer that is using the excel export functionality. What is the width value to use as parameter here to get the wanted points width?
I am making a drawing app where the user can erase what they have drawn.
One of the neat ways I am trying to figure out how they can can erase aside from backtracking where they've touched is to erase an enclosed area they have designated.
The User Story would be the :
- user would draw something that resembles an enclosed area
- If it is an enclosed area, mark the area as ready to delete
- User clicks on delete
- the pixels inside the region are written with the delete color
I've seen this done in other drawing apps. Math is not my area of expertise. I'm not even sure where to look for this answer.
The app already has the ability to drawLine()'s on a bitmap
So judging from the revised question in the comments: "given a sequence of line segments where each segment starts where the previous ends, do the lines form (roughly) a closed polygon?" this should not be too difficult.
Simply take the head of the first segment and compare it to the tail of the last segment. If they are sufficiently close together, then the segments roughly form a closed (possibly self-intersecting) polygon.
So if you have n segments formed by the sequence of points p_0,...,p_n, you can consider the Euclidean distance between p_0 and p_n. The idea is as follows:
if dist(p_0,p_n) < THRESHOLD
create a new segment [p_n,p_0] closing off the polygon
delete pixels in the enclosed region
dist(p_0,p_n) can be implemented as sqrt((p_0x - p_nx)^2 + (p_0y - p_ny)^2). The variable THRESHOLD will be some constant determining how much error you want to tolerate between the first point and the last point.
For my project i am writing an image pre processing library for scanned documents. As of now I am stuck with line removal feature.
Problem Description:
A sample scanned form:
Name* : ______________________________
Age* : ______________________________
Email-ID: |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
Note:
Following are the further conditions:
The scanned document may contain many more vertical and horizontal guiding lines.
Thickness of the lines may exceed 1px
The document itself is not printed properly and might have noise in the form of ink bloating or uneven thickness
The document might have colored background or lines
Now what I am trying to do is to detect these lines and remove them. And while doing so the hand written content should not be lost.
Solution so for:
The current solution is implemented in Java.
Detected these lines by using a combination of canny/sobel edge detectors and a threshold filter(to make image bitonal). From the previous action I get a black and white array of pixels. Traverse the array and check whether lumanicity of that pixel falls below a specified bin value. And if I found 30 (minimum line length in pixels) such pixels, I remove them. I repeat the same for vertical lines but considering the fact there will be cuts due to horizontal line removal.
Although the solution seems to work. But there are problems like,
Removal of overlapping characters
If characters in the image are not properly spaced then it is also
considered as a line.
The output image from edge detection is in black and white.
A bit slow. Normally takes around 40 seconds for image of 2480*3508.
Kindly guide how to do it properly and efficiently. And if there is an opensource library then please direct.
Thanks
First, I want to mention that I know nothing about image processing in general, and about OCR in particular.
Still, a very simple heuristic comes to my mind:
Separate the pixels in the image to connected components.
For each connected component decide if it is a line or not using one or more of the following heuristics:
Is it longer that the average letters length?
Does it appear near other letters? (To remove ink bloats or artifacts).
Does its X gradient and Y gradient large enough? This could make sure that this connected component contains more than just horizontal line.
The only problem I can see is, if somebody writes letters on a horizontal line, like so:
/\ ___
/ \ / \
|__| |___/
-|--|---|---|------------------
| | \__/
In that case the line would remain, but you have to handle this case anyhow.
As I mentioned, I'm by no means an image processing expert, but sometimes very simple tricks work.
First problem: You have 400 pixels width to go on, and need to fit some text within that constraint as large as possible (thus, the text shall use that amount of space).
Throw in a new constraint: If the text is just "A", then it shall not zoom this above 100 pixels height (or some specific font size).
Then, a final situation: Linebreaks. Fit some text in the largest possible way within e.g. 400 x 150 pixels.
An obvious way is to simply start with point 1, and then increase until you can't fit it anymore. This would work for all three problems, but would be very crude. The fitting of a single line within bounds could be done by writing it with some fixed point size, check the resulting pixel bounds of the text, and then simply scale it with a transform (the text scales properly too then, check out TransformUI).
Any ideas of other ways to attack this would be greatly appreciated!
As what you are modelling is complex, especially with line breaks, then your initial proposal of trying all sizes is along the right lines, especially if it needs to be accurate.
However, rather than testing each value, you can use a binary search to find the appropriate font size. You know the size is somewhere between 1 and 100 (your upper range). using a binary search, each test sets the font size and checks the resulting layout. If the text is too large, then we search the lower half of the current range of possible values. If the font size fits, then we search the upper half. Your search will use at most 7 attempts (100 log base 2 rounded up), it will be exact, finding the largest size without going over, and it will be flexible if you need to add more requirements later, such as a mix of fonts or more stringent constraints on the layout.
I'm assuming you are using a text component that does line wrapping, and that you can set the maximum width to 400. So, you set the font size and it does the layout giving you back the required height, laying out text within the given width.
You can use hints to try to guide the algorithm to the result quicker, such as making your first guess close to the expected size, but text rendering is fast, that the performance increase may not be worth the implementation effort.
See Wikipedia - Binary Search Algorithm
I would do the following:
Assume you want W pixels wide text.
Pick an arbitrary size, say 10pt, and see what bounding box the text-string gets for that size. Lets say it gets N pixels wide.
Set the new size to 10pt * W/N, and repeat from step one, until you get within a reasonable threshold. (Hopefully it would work within one iteration.)
This relies on the fact that the width of the string, is roughly proportional to the size of the font.
I'd instantiate the Font at the largest desired size: say 72 for one inch glyphs at 72 dpi. Use TextLayout to get the bounds and scale using AffineTransform (direct) or AffineTransformOp (offscreen), while preserving the aspect ratio. Suitable RenderingHints help, too.