我使用下面的代码进行对象检测。
int main(int argc, char* argv[]){
VideoCapture cap(0);
if (!cap.isOpened()){
cout << "Cannot open the video cam" << endl;
return -1;}
int totalFrameNumber = cap.get(CV_CAP_PROP_FRAME_COUNT);
Mat frame;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
while (1)
{
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat frame2;
Rect rectangle2(420,280, 40, 40);
rectangle(frame, rectangle2, Scalar(255, 255, 255));
Mat cornerstrength;
cornerHarris(frame, cornerstrength, 3, 3, 0.1);
//threshold the corner strength
Mat harriscorners;
double th = 0.00001;
threshold(cornerstrength, harriscorners, th, 255, THRESH_BINARY);
morphologyEx(harriscorners, harriscorners, MORPH_CLOSE, Mat(), Point(-1, -1), 6);
//local maxima detection
Mat dilated, localMax;
dilate(cornerstrength, dilated, Mat());
compare(cornerstrength, dilated, localMax, CMP_EQ);
threshold(cornerstrength, harriscorners, th, 255, THRESH_BINARY);
harriscorners.convertTo(harriscorners, CV_8U);
bitwise_and(harriscorners, localMax, harriscorners);
harriscorners.convertTo(harriscorners, CV_32F);
Mat S(0, 2, CV_32SC1);
//drawing a circle around corners
for (int j = 0;j < harriscorners.rows;j++)
for (int i = 0;i < harriscorners.cols;i++)
{
if (harriscorners.at<float>(j, i)> 0)
{ circle(frame, Point(i, j), 5, Scalar(255), 2, 8);
Mat pt(1, 2, CV_32SC1);
pt.at<int>(1, 0) = i;
pt.at<int>(0, 1) = j;
// Add the point to S
S.push_back(pt);
for (int x = 430; x < 460; x++)
for (int y = 285; y < 315; y++)
if ((pt.at<int>(1, 0) = i) == x && (pt.at<int>(0, 1) = j) == y))
{
Rect rectangle2(430, 285, 30,30);}}}
imshow("MyVideo", frame);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
我想当条件 if((pt.at(1,0)= i)== x&amp;&amp;(pt.at(0,1)= j)== y))< / strong>然后建立矩形2的大小 rectangle2(420,280,40,40)更改为 rectangle2(430,285,30,30)。我可以执行此更改但是当我使用我的代码时两个矩形(前一个矩形2和new rectangle2)显示在picture.but我想显示onle new rectangle2.do你有任何想法解决我的问题?非常感谢..
答案 0 :(得分:0)
正如我在评论中所说,你需要创建一个克隆并保持框架几乎不变。
所以,在这一部分:
Rect rectangle2(420,280, 40, 40);
rectangle(frame, rectangle2, Scalar(255, 255, 255));
Mat cornerstrength;
cornerHarris(frame, cornerstrength, 3, 3, 0.1);
写一些更像这样的东西
cv::Mat frameCopy = frame.clone();
Rect rectangle2(420,280, 40, 40);
rectangle(frameCopy , rectangle2, Scalar(255, 255, 255));
Mat cornerstrength;
cornerHarris(frameCopy , cornerstrength, 3, 3, 0.1);
然后,在这部分中,我不确定它的目的是什么
for (int x = 430; x < 460; x++)
for (int y = 285; y < 315; y++)
if ((pt.at<int>(1, 0) = i) == x && (pt.at<int>(0, 1) = j) == y))
{
Rect rectangle2(430, 285, 30,30);
}
}
}
imshow("MyVideo", frame);
但是你可能想用新的矩形显示新图像,所以你可以再做一次像以前那样:
cv::Mat anotherCopy= frame.clone();
Rect rectangleInLoop(430,280,30,30);
rectangle(anotherCopy, rectangleInLoop, Scalar(255, 255, 255));
imshow("MyVideo", anotherCopy);