openCV java compareHist() value larger than 1 - java

I am learning openCV and when I tried to compare 2 histograms and print the result the values coming were actually larger than 1. Sorry if it is a stupid question but I am still learning.
Output showing those values:
enter image description here
as you can see in the image the values are in thousands .
Thanks a lot

The third argument of the compareHist method selects the comparison function. In your case, the Chi-Square function is used to compare the two histograms, which does not have an upper limit. You could use the correlation comparison function (CV_COMP_CORREL, 0), which is bound between -1 and 1. See also this question on the OpenCV stack exchange.

Related

how to extract numeric values from jpg file using OCR

I have created the below code and executed:
Ocr.setUp();
Ocr ocr = new Ocr();
ocr.startEngine("eng", Ocr.SPEED_FASTEST);
String s = ocr.recognize(theImage, Ocr.RECOGNIZE_TYPE_ALL, Ocr.OUTPUT_FORMAT_PLAINTEXT);
ocr.stopEngine();
Output:
Result: L‘i
L‘L’Ui l
Actually the image only contains the numeric values. Is it possible to extract only numeric value by using above code?
I have 1234 numeric value displayed in jpg file, I just want to print that numeric value in o/p console. Can anybody help me out?
I have a few technology-independent observations about your code.
"SPEED_FASTEST" indicates your preference for fast OCR. Fast is the opposite of High-quality. You either get speed or quality. If the image is clear - no problem, but if the image is less than perfect, Quality mode will have more algorithms to deal with defects.
Nowhere in your code it is specified that you constrain the character set to digits only. If you do not indicate the language or character set, then usually by default the entire English character set is used. See my response on this post here: OCR why not find only character
Typically if you post a sample image along with your question and code, contributors can give you better advice.

Unexpected results from Metaphone algorithm

I am using phonetic matching for different words in Java. i used Soundex but its too crude. i switched to Metaphone and realized it was better. However, when i rigorously tested it. i found weird behaviour. i was to ask whether thats the way metaphone works or am i using it in wrong way. In following example its works fine:-
Metaphone meta = new Metaphone();
if (meta.isMetaphoneEqual("cricket","criket")) System.out.prinlnt("Match 1");
if (meta.isMetaphoneEqual("cricket","criketgame")) System.out.prinlnt("Match 2");
This would Print
Match 1
Mathc 2
Now "cricket" does sound like "criket" but how come "cricket" and "criketgame" are the same. If some one would explain this. it would be of great help.
Your usage is slightly incorrect. A quick investigation of the encoded strings and default maximum code length shows that it is 4, which truncates the end of the longer "criketgame":
System.out.println(meta.getMaxCodeLen());
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));
Output (note "criketgame" is truncated from "KRKTKM" to "KRKT", which matches "cricket"):
4
KRKT
KRKT
KRKT
Solution: Set the maximum code length to something appropriate for your application and the expected input. For example:
meta.setMaxCodeLen(8);
System.out.println(meta.encode("cricket"));
System.out.println(meta.encode("criket"));
System.out.println(meta.encode("criketgame"));
Now outputs:
KRKT
KRKT
KRKTKM
And now your original test gives the expected results:
Metaphone meta = new Metaphone();
meta.setMaxCodeLen(8);
System.out.println(meta.isMetaphoneEqual("cricket","criket"));
System.out.println(meta.isMetaphoneEqual("cricket","criketgame"));
Printing:
true
false
As an aside, you may also want to experiment with DoubleMetaphone, which is an improved version of the algorithm.
By the way, note the caveat from the documentation regarding thread-safety:
The instance field maxCodeLen is mutable but is not volatile, and accesses are not synchronized. If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization is used to ensure safe publication of the value between threads, and must not invoke setMaxCodeLen(int) after initial setup.

Taking random defined Strings

I want to do an app that, in function of the parameters checked, gives me a random String that I previusly set. The problem isn't the design of the app so the problem is the methods or classes that do what I want.
-I explain my objective with an example:
We have 100 plans to do this weekend. They are in a box and I want to pick randomly one. So great, I suppose that would be more or less easy, but, this weekend is raining so I want to pick a plan that it could be done even if it rains. And it's ending the month and I don't want to spend money so a free plan would be great. Then, I check the boxes "Free plan" and "Raining" and the app gives to me a random plan that meet the conditions.
How would you do that?
I was thinking about a bigs arrays like "Raining plans", "Free plans", with strings inside and when you check the two boxes the arrays were mixed and a string was randomly picked but I don't know how to do that and I think it could be done in another easiest way.
Thanks in advance.
Do with a two dimentional array as follows:
arr[filter][tasks];
let the filter values be binary strings converted to int
say you have two filters as aforementioned "rainy" and "free", the possible vaues are
00 - 0(decimal value 0) - not rainy, not free
01 - 1(decimal value 1) - not rainy, free
10 - 2(decimal value 2) - rainy, not free
11 - 3(decimal value 3) - rainy, free
when user checks both the values, then you have 1 and 1. Now convert binary 11 to integer which would be your first index. Get the random second index for the task.
You can have any number of filters
Hint: Integer.parseInt(binary_str, 2) for binary to int conversion.

What is the equivalent Anylogic function "zidz" in Java?

In Anylogic there is a built-in function zidz, which does the following:
zidz(double a, double b)
Description
Tries to divide the first argument by the second. If the result is infinity or not a number, returns 0, otherwise returns the division result.
My question for you guys is: do you know a function that does the same operation but in Java?
I've searched and come up with Math#IEEEremainder, but I don't think it does the exact same thing as 'zidz'.
There isn't one in the common set of libraries. That's not to say there is nobody in the world who has written and published one, but the function is so trivial as to be unlikely to be included in a Java library.

Opencv Imgproc.HoughLines() tuning length parameters

--See update/answer below. User error!--
I'm trying to understand how to set the parameters in Imgproc.HoughLines() to find shorter lines. I've tried something like this that doesn't work at all:
Imgproc.HoughLines(matSrc, matLines, 1, Math.PI / 180, houghThreshCurrent, 25, 10);
I have tried several values for the last two parameters, but none seem to work--it finds no lines. However, using the version of the method without the last two parameters does a decent job of finding the lines I want, just not the shorter lines no matter how low the threshold is.
Here's the doc for the last two params:
srn For the multi-scale Hough transform, it is a divisor for the distance resolution rho. The coarse accumulator distance resolution is rho and the accurate accumulator resolution is rho/srn. If both srn=0 and stn=0, the classical Hough transform is used. Otherwise, both these parameters should be positive.
stn For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
Could someone translate or provide example values for that? :)
I've also tried the probabalistic version, HoughLinesP(). It doesn't seem to work very well for my use case. The other option would be to scale my image to a larger size where the default HoughLines() works if I can't get the line distance parameters working.
Answer: My problem was I didn't realize the Mat returned by HoughLinesP() was in a different format than the one returned by HoughLines(). I was transforming the results from HoughLinesP() from polar coordinates when they were already in the XY coordinates! Turns out HoughLinesP() is far superior for our needs and its parameters work great for tuning line length. Here's the link that helped me see the error of my ways: https://dsp.stackexchange.com/questions/10467/influence-of-image-size-to-edge-detection-in-opencv
A very good example can be found here: http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html
Try using CV_PI instead of Math.PI? Other reasons could be because of your threshold. Try inserting a value like 50(play around with the numbers). The last 2 values, u can try leaving it to be zero and test it first before inserting values. Default Values for the last 2 is usually zero.
There could be many reasons why its not working, so let's slowly find the cause one by one. Also, you did Canny it or something before you applied Hough right?
Hope that helps, let me know if my suggestions are useful and helped(: Cheers.

Categories

Resources