我是编程新手。我想使用openCV制作一个程序来拍摄图像并对其进行处理。我已经调试了所有错误和警告,但是清除错误和警告后,仍然存在问题细分问题
我们已经尝试了很多方法,但是仍然可以解决此问题。我们使用C ++编程语言。
这是我的代码:
using namespace cv;
using namespace std;
Mat src;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);
/// Function header
/**
* @function main
*/
int main(int, char** argv) {
while (true) {
void thresh_callback(int, void*);
time_t timer_begin, timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount = 5;
// set camera params
Camera.set(CV_CPU_POPCNT, CV_8UC1);
// Open camera
// cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {
cerr << "Error opening the camera" << endl;
return -1;
}
// Start capture
// cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time(&timer_begin);
for (int i = 0; i < nCount; i++) {
Camera.grab();
Camera.retrieve(image);
if (i % 5 == 0)
; // cout<<"\r captured "<<i<<" images"<<std::flush;
}
// cout<<"Stop camera..."<<endl;
Camera.release();
// show time statistics
time(&timer_end); /* get current time; same as: timer = time(NULL) */
double secondsElapsed = difftime(timer_end, timer_begin);
// cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< (
// float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
// save image
cv::imwrite("gbrkayu.jpg", image);
// cout<<"gbrkayu.jpg"<<endl;
// Load source image and convert it to gray
// char* "home/pi/BoundingBox/kayu.jpg"
src = imread("gbrkayu.jpg", 1);
resize(src, src, Size(160, 120));
// Convert image to gray and blur it
cvtColor(src, src_gray, COLOR_BGR2GRAY);
blur(src_gray, src_gray, Size(3, 3));
// Create Window
const char* source_window = "Source";
namedWindow(source_window, WINDOW_AUTOSIZE);
imshow(source_window, src);
createTrackbar(" Threshold:", "Source", &thresh, max_thresh,
thresh_callback);
thresh_callback(0, 0);
waitKey(10);
// return(0);
}
}
/**
* @function thresh_callback
*/
void thresh_callback(int, void*) {
printf("\t Info: Panjang dan lebar \n");
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
// Detect edges using Threshold
threshold(src_gray, threshold_output, thresh, 255, THRESH_BINARY);
// Find contours
findContours(threshold_output, contours, hierarchy, RETR_TREE,
CHAIN_APPROX_SIMPLE, Point(0, 0));
// Find the rotated rectangles and ellipses for each contour
vector<RotatedRect> minRect(contours.size());
vector<RotatedRect> minEllipse(contours.size());
for (size_t i = 0; i < contours.size(); i++) {
minRect[i] = minAreaRect(Mat(contours[i]));
if (contours[i].size() > 5) {
minEllipse[i] = fitEllipse(Mat(contours[i]));
}
}
// Get the moments
vector<Moments> mu(contours.size());
for (int i = 0; i < contours.size(); i++) {
mu[i] = moments(contours[i], false);
}
// Get the mass centers:
vector<Point2f> mc(contours.size());
for (int i = 0; i < contours.size(); i++) {
mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
}
// Draw contours + rotated rects + ellipses
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
Mat font = Mat::zeros(threshold_output.size(), CV_8UC3);
// float* Diameter;
// float value;
for (size_t i = 0; i < contours.size(); i++) {
Scalar color =
Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
// contour
drawContours(drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0,
Point());
// ellipse
ellipse(drawing, minEllipse[i], color, 2, 8);
// rotated rectangle
Size2f rect_size = minRect[i].size;
Size rect_size2 = minRect[i].size;
Point2f rect_points[4];
minRect[i].points(rect_points);
for (int j = 0; j < 4; j++) {
line(drawing, rect_points[j], rect_points[(j + 1) % 4], color, 1, 8);
}
float lebar1 = rect_size.width;
float panjang1 = rect_size.height;
int delayms = 0;
// Mencari diameter terkecil
// Jika panjangnya merupakan diameter terkecil
if (lebar1 > panjang1 and panjang1 > 40) {
panjang1 = panjang1 / 4.643;
lebar1 = lebar1 / 4.7093;
float panjang2 = panjang1;
float lebar2 = lebar1;
string box_text = format("D : %.0f\n", panjang2);
putText(drawing, box_text, Point(10, 10), FONT_HERSHEY_PLAIN, 1.0,
CV_RGB(255, 255, 255), 2.0);
putText(font, box_text, Point(10, 40), FONT_HERSHEY_PLAIN, 3.0,
CV_RGB(255, 255, 255), 2.0);
// displayOverlay("Contours",box_text,0);
printf(" * Value :Width= %.2f - Height= %.2f \n", lebar1, panjang1);
printf(" * Pembulatan:Width= %.0f - Height= %.0f \n", lebar2,
panjang2);
float value = panjang2;
}
// Jika lebarnya merupakan diameter terkecil
if (lebar1 < panjang1 and lebar1 > 40) {
panjang1 = panjang1 / 4.7093;
lebar1 = lebar1 / 4.643;
float panjang2 = panjang1;
float lebar2 = lebar1;
string box_text = format("D : %.0f\n", lebar2);
float value = lebar2;
putText(drawing, box_text, Point(10, 10), FONT_HERSHEY_PLAIN, 1.0,
CV_RGB(255, 255, 255), 2.0);
putText(font, box_text, Point(10, 40), FONT_HERSHEY_PLAIN, 3.0,
CV_RGB(255, 255, 255), 2.0);
// displayOverlay("Contours",box_text,0);
printf(" * Value :Width= %.2f - Height= %.2f \n", lebar1, panjang1);
printf(" * Pembulatan:Width= %.0f - Height= %.0f \n", lebar2,
panjang2);
}
}
// Show in a window
namedWindow("Grade", WINDOW_NORMAL);
resizeWindow("Grade", 160, 30);
moveWindow("Grade", 160, 30);
imshow("Grade", font);
namedWindow("Contours", WINDOW_AUTOSIZE);
moveWindow("Contours", 160, 90);
imshow("Contours", drawing);
}
请帮助,我将非常感谢。