Floating point exception when looping and reading images
I'm building a line following robot with OpenCV (c++), Raspberry Pi, Arduino, and an Arducam. I keep getting a Floating point exception when I decrease the delay time between image scans (at the end of the loop in main(), down to a 1 second delay). A bash script simultaneously captures an image from the Arducam, and the c++ program continuously reads the same image file name that's being written over.
At first I thought the exception was thrown because the image file was not finished being written to before it was read, but I wrote the loadImage() method in a way where (I hope) the method would not return until the data is complete. It could be wrong, I am just looking for a way to avoid or ignore the Floating point exception.
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#define ADDRESS 0x04 // The Arduino I2C address
static const char *devName = "/dev/i2c-1"; // The I2C bus
using namespace cv;
using namespace std;
// Line Detection program
// Efficient method to reduce noise
Mat& scanAndReduce(Mat& I, const int* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
int channels = I.channels();
int nRows = I.rows;
int nCols = I.cols * channels;
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}
}
return I;
}
// Loads command-line argument image file location
int loadImage(Mat& source, char argv[])
{
while ( !source.data )
{
source = imread(argv, 0);
}
return 0;
}
// Displays input image in new window
void display(Mat& image)
{
namedWindow("Result", WINDOW_NORMAL);
imshow("Result", image);
waitKey(0);
}
// prints 10x10 ellipse with input center and height to input matrix
void drawEllipse(Mat& destination, int center, int lineY)
{
ellipse( destination, // destination matrix
Point(center,lineY), // location
Size(20,20), // size
0, // angle
0, // start
360, // end
Scalar(100,0,0)); // color
}
// Scan bottom half of screen to see where line is
int scanLines(Mat& sourceImage)
{
int diff = (sourceImage.rows) * 0.1; // distance between scan lines
int lineY = (sourceImage.rows) * 0.9; // starting scan line
int vals[sourceImage.cols]; // save line values (left to right)
int center = 0; // sums indexes of dark values
int numDark = 0; // tracks dark values
int offset = 0;
// get values along scan lines
for(int i = 0; i < 5; i++) // traverse y axis (bottom to top)
{
center = 0; // sums indexes of dark values
numDark = 0; // tracks dark values
Scalar intensity;
int leftBound = sourceImage.cols * 0.25;
int rightBound = sourceImage.cols * 0.75;
for(int j = leftBound; j < rightBound; j++) // traverse x axis of image
{
intensity = sourceImage.at<uchar>(Point(j,lineY)); // save value
vals[j] = intensity[0]; // get first value (greyscale)
// save dark value
if(vals[j] < 100)
{
center += j; // add index to sum
numDark++; // increment dark ...
Why not pass the image (as an argument) to the c++ program that you call from the script?
Thanks for the suggestion! I tried it out and it's actually quite an improvement. I am still getting a floating point exception, but at least the program will continue running because the script keeps going. Now it seems like I am getting the exception becuase one of the image processing methods is not liking certain values. This also probably means that the image not being loaded is less of the problem, but this method is much better than before. I guess this problem is mostly solved now, since I have a way to ignore the error and just need to figure out how process the images correctly.
Yep, the scanLines() method is throwing the exception. I am not sure exactly why, but I needed to replace that method that has less issues overally anyways.