error: expected '} before else - eclipse

I am using eclipse and I will ask a stupid question: why the error? code should be ok. I guess I'm just getting old. I just don't see the error. all the {} match. What the code does it sets the hemisphere for geolocation in a flight simulator, as the math should not have negative numbers.
so here goes:
O_lat is tested for zero or a positive number
if positive don't mess with it. and make quadrant = 1
else its negative so we make it positive and make the quadrant = 0
that's it.
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 ){
{ vns1 = 1; }
else {
vns1 = 0;
O_lat = O_lat * -1; }}

All the braces match, but the else is not where it should be for the if.
Remove one of the superfluous braces:
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 ){
vns1 = 1;
}
else {
vns1 = 0;
O_lat = O_lat * -1;
}

Just re-arrange the code a bit:
int vns1, vew1, vns2, vew2;
if( O_lat >= 0 )
{
{ //this is unnecessary
vns1 = 1;
}
else
{
vns1 = 0;
O_lat = O_lat * -1;
}
}//this is unnecessary too

Related

cv::matchShapes method implementation giving error

My objective is to find the pattern in the image captured from camera for this I found the cv::matchShapes method of opencv to implement it.
When I have implemented this method it is making the app crash on matchShapes method.Might be I have done something wrong:-
I exactly do not know how to use this method in order to find the match shape in my query image.This is what I tried.
Here is my code:-
- (void)tryingMatchShapes:(cv::Mat)_image _image1:(cv::Mat)_image1
{
std::vector<std::vector<cv::Point> > squares;
cv::Mat pyr, timg, gray0(_image.size(), CV_8U), gray;
int thresh = 50, N = 11;
cv::pyrDown(_image, pyr, cv::Size(_image.cols/2, _image.rows/2));
cv::pyrUp(pyr, timg, _image.size());
std::vector<std::vector<cv::Point> > contours;
for( int c = 0; c < 3; c++ ) {
int ch[] = {c, 0};
mixChannels(&timg, 1, &gray0, 1, ch, 1);
for( int l = 0; l < N; l++ ) {
if( l == 0 ) {
cv::Canny(gray0, gray, 0, thresh, 5);
cv::dilate(gray, gray, cv::Mat(), cv::Point(-1,-1));
}
else {
gray = gray0 >= (l+1)*255/N;
}
cv::findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
}
}
cv::Mat pyr1, timg1, gray2(_image1.size(), CV_8U), gray1;
cv::pyrDown(_image1, pyr1, cv::Size(_image1.cols/2, _image1.rows/2));
cv::pyrUp(pyr1, timg1, _image1.size());
std::vector<std::vector<cv::Point> > contours1;
for( int c = 0; c < 3; c++ ) {
int ch[] = {c, 0};
mixChannels(&timg1, 1, &gray2, 1, ch, 1);
for( int l = 0; l < N; l++ ) {
if( l == 0 ) {
cv::Canny(gray2, gray1, 0, thresh, 5);
cv::dilate(gray1, gray1, cv::Mat(), cv::Point(-1,-1));
}
else {
gray1 = gray2 >= (l+1)*255/N;
}
cv::findContours(gray1, contours1, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
}
}
for( size_t i = 0; i < contours.size(); i++ )
{
double value= cv::matchShapes(contours[i], contours1[i], CV_CONTOURS_MATCH_I1, 0);
NSLog(#"%f",value);
}
}
UIImage *testImage = [UIImage imageNamed:#"note_with_marks (1).png"];
[self.imageView setImage:testImage];
cv::Mat forground = [testImage CVMat];
UIImage *testImage2 = [UIImage imageNamed:#"1.png"];
cv::Mat forground2 = [testImage2 CVMat];
[self tryingMatchShapes:forground _image1:forground2];
The app is getting crashed.
Error:-
OpenCV Error: Assertion failed (contour1.checkVector(2) >= 0 && contour2.checkVector(2) >= 0 && (contour1.depth() == CV_32F || contour1.depth() == CV_32S) && contour1.depth() == contour2.depth()) in matchShapes, file /Users/Aziz/Documents/Projects/opencv_sources/trunk/modules/imgproc/src/contours.cpp, line 1705
Please some body help me in implementing this method from coding point of view.
Please I need some coding help.I have already gone through I lot of theoretical concepts.
Thanks in advance!
std::vector<std::vector<cv::Point> > contours1;
You can't apply matchShapes to such type. You can apply it for each (one) contour, not for group (array) of contours. For example:
cv::matchShapes(contours[0], contours1[0], CV_CONTOURS_MATCH_I1, 0);

Illegal division by zero

#xyVal = (4,4,6,6,10,12,18,22,24,28,30);
#yVal = (176,178,180,184,192,202,210,218,224,232,238);
#xxVal = (9,9,9,9,9 ,11,13,15,17,19,19);
#xVal = (168,166,164,162,158,150,142,134,122,116,110);
for ($i = 0; $i < scalar(#xVal); $i++){
for ($i = 0; #xyVal[$i] < #xxVal[$i]; $i++){
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
}
}
print #yNewVal;
I understand why its giving me the error Illegal division by zero about line 9 (the #yNewVal = ...)
I want the array to have 0 in it if there is a division between zeros. What am I doing wrong? So, how can I avoid that my application crashes when there is a division by zero?
Your divisor on that line is #xyVal[$i] - #xyVal[$i-1], so any case where you have two identical adjacent values in #xyVAl (e.g. 4,4) will result in a 0, and thus a divide-by-zero error.
You could say:
#yNewVal = ($_ = #xyVal[$i] - #xyVal[$i-1]) == 0 ? 0 : #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/$_;
well if i understand you correctly:
if (#xyVal[$i] == #xyVal[$i-1])
#yNewVal = 0;
else
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
You can perform a try/catch using eval and conditional operators.
eval {
#yNewVal = #yVal[$i-1] + (#yVal[$i] - #yVal[$i-1])*(#xxVal[$i] - #xyVal[$i-1])/(#xyVal[$i] - #xyVal[$i-1]);
1;
} or do {
#yNewVal = (0);
};
print #yNewVal;
Though, your phrase is returning a scalar value and putting it into an array variable. So you may want to re-factor that.

Return zero or positive number?

I was initially thinking that the code below would return 0, my question, is there a function that I can use to only receive zero/positive results here?
NSUInteger pruneSize = 5 - 20; // Returns: -15
Currently I am just checking the result myself, but was wondering if I was missing something simpler.
NSUInteger pruneSize = 5 - 20;
if(pruneSize >= 0) {
// Do zero/positive Stuff ...
}
pruneSize >= 0 is always true as pruneSize is unsigned. You should get a warning here. You need to change the type to NSInteger, that is the signed integer. If you want to clip the lower value to zero for a signed int then you can do this:
NSInteger pruneSize = 5 - 20; // signed int
pruneSize = pruneSize < 0 ? 0 : pruneSize;
You can use abs(pruneSize) which will return you positive or zero number in any case.
EDIT:
NSUInteger pruneSize = 5-20;
if(pruneSize < 0)
{
pruneSize = 0;
}
NSLog(#"%d",pruneSize);
Hope this helps you.
If you want your function to return always zero if your result is in negative(less than 0) then return zero or else return result
int n=0;
if(result > 0){ //positive
n = result
else
n = 0
return n
or use the abs method

finding the left substring is equal to right substring

Write a function which given a string S returns the index (counting from 0) of character such that the substring on its left is a reversed susbstring on its right (or -1 if such an index does not exist).
For example, given a string
racecar
Function should return 3, because the substring on the left of the character e at index 3 is rac, and the one on the right is car.
get the length/2 and verify lengths first and then if the lengths are same then reverse the first half and compare with the second.
Example function:
private int TestMethod1(string str)
{
if (str.Length > 0)
{
if (str.Length % 2 != 0)
{
string strFront = string.Empty;
for (int i = (str.Length / 2) - 1; i >= 0; i--)
{
strFront += str.Substring(i, 1);
}
if (strFront.Equals(str.Substring((str.Length / 2) + 1)))
{
return str.Length / 2;
}
}
}
return -1;
}

iPhone time comparison

I am trying to figure out how to create an 'if' statement that uses a time value as a condition. For example:
if (time <= 10:00) {
score = 3;
} else if (time <= 20:00) {
score = 5;
} else {
score = 9;
}
I know that a string of "5:23" cannot be compared this way but I don't think I can just turn a string value like that directly into an integer. Any thoughts?
Try this:
NSString *realTimeString = #"10:00";
NSString *someTimeString = [realTimeString stringByReplacingOccurrencesOfString:#":"
withString:#"."];
float time = [someTimeString floatValue];
if (time <= 10.00) {
score = 3;
} else if (time <= 20.00) {
score = 5;
} else {
score = 9;
}