since i have only one karma i cannot upload images
the video is played on a white background with most of its area covered by the rectangle shape thing
pls help me with a solution
#include
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#include
#include
using namespace cv;
using namespace std;
Mat imgOriginal;
int largest_area = 0;
int largest_contour_index = 0;
Rect bounding_rect;
int theObject[2] = { 0,0 };
Rect bounding_rect = Rect(0, 0, 0, 0);
void searchForMovement(Mat thresholdImage, Mat &cameraFeed)
{
bool objDetected = false;
Mat temp;
thresholdImage.copyTo(temp);
vector>contours;
vectorhierarchy;
findContours(temp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
printf("%ld\n", contours.size());
if (contours.size() > 0) objDetected = true;
else objDetected = false;
if (objDetected)
{
for (size_t i = 0; i< contours.size(); i++) // iterate through each contour.
{
double area = contourArea(contours[i]); // Find the area of contour
if (area > largest_area)
{
largest_area = area;
largest_contour_index = i; //Store the index of largest contour
bounding_rect = boundingRect(contours[i]); // Find the bounding rectangle for biggest contour
}
}
}
Scalar color = Scalar(0, 0, 255);
drawContours(imgOriginal, contours, -1, color);
Mat roi = Mat(imgOriginal, bounding_rect);
// show the images
imshow("result", imgOriginal);
imshow("roi", roi);
Size size(411, 405);
Mat dst;
resize(roi, dst, size);
imwrite("result.png", dst);
//Mat q, w;
Mat e = imread("result0.png");
namedWindow("X", WINDOW_AUTOSIZE);
imshow("X", e);
cv::Mat Q(dst.size(), dst.type());
cv::threshold(dst, Q, 100, 255, cv::THRESH_BINARY);
cv::Mat W(e.size(), e.type());
cv::threshold(e, W, 100, 255, cv::THRESH_BINARY);
Mat Solar = Q - W;
int count_black = 0;
int count_white = 0;
for (int y = 0; y < Solar.rows; y++) {
for (int x = 0; x < Solar.cols; x++) {
if (Solar.at(y, x) == cv::Vec3b(255, 255, 255)) {
count_white++;
}
else if (Solar.at(y, x) == cv::Vec3b(0, 0, 0)) {
count_black++;
}
}
}
cout << count_black << endl;
cout << count_white << endl;
//long float g = 0;
//g = count_black / count_white;
//cout << g;
int x = theObject[0];
int y = theObject[1];
circle(cameraFeed, Point(x, y), 20, Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x, y - 25), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x, y + 25), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x - 25, y), Scalar(0, 255, 0), 2);
line(cameraFeed, Point(x, y), Point(x + 25, y), Scalar(0, 255, 0), 2);
}
int main(int argc, char** argv)
{
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Cannot open camera" << endl;
return -1;
}
namedWindow("Control", WINDOW_AUTOSIZE);
while (true)
{
bool bSuccess = cap.read(imgOriginal);
if (!bSuccess)
{
cout << "Cannot read frame from video" << endl;
break;
}
Mat image, imgThreshold;
cvtColor(imgOriginal, image, CV_BGR2GRAY);
threshold(image, imgThreshold, 150, 255, THRESH_BINARY_INV);
searchForMovement(imgThreshold, imgOriginal);
imshow("Thresholded Image", imgThreshold);
imshow("Original", imgOriginal);
if (waitKey(30) == 27)
{
cout << "Esc key pressed" << endl;
break;
}
}
return 0;
}
↧