[英]Microsoft Visual Studio C++, OpenCV animation
我有一個問題,我不太確定如何克服它。 我想創建一個動畫,以(流暢地)改變我提取的輪廓的顏色。 我假設我要做的就是使用while循環(對於動畫),並更改r,g,b變量之一(與for循環一起使用)的值,但是我不確定如何正確地做到這一點。
提前致謝!
using namespace cv;
using namespace std;
int main()
{
Mat OurImage, img, bin, anim, gray;
string Destination = "rot.jpg";
OurImage = imread(Destination, CV_LOAD_IMAGE_COLOR);
if(! OurImage.data)
{
printf("No image!");
getchar();
return -1;
}
int r = 0, g = 255, b = 255;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
cvtColor(OurImage,gray,CV_RGB2GRAY);
Canny( gray, img, 100, 200,3);
findContours(img,contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
Mat drawing = Mat::zeros(img.size(),CV_8UC3);
for(int i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(r, g, b);
drawContours(drawing, contours,i,color,2,8,hierarchy, 0,Point());
}
namedWindow("WINDOW", CV_WINDOW_AUTOSIZE);
while(true)
{
沒什么想法,環顧這里
printf(".");
anim = drawing.clone();
r = r+5;
imshow("WINDOW",anim);
if(waitKey(20) == 27)
break;
}
waitKey(0);
}
編輯
我設法使它從黃色變為白色(從0到255),但是現在我不知道如何使它返回並強制程序執行該操作,直到我單擊為止。 我是否使用標志/如果? 另一個?
while(true)
{
for(int i = 0; i < contours.size(); i++)
{
r=r+5;
printf(".");
Scalar color = Scalar(r, g, b);
drawing = drawing.clone();
drawContours(drawing, contours,i,color,2,8,hierarchy, 0,Point());
}
imshow("WINDOW",drawing);
if(waitKey(20) == 27)
break;
}
該代碼將連續改變邊緣的顏色,從白色變為黃色,然后再變為黃色。
您可以delay
更改動畫速度。
請注意,您可以使用帶有邊緣蒙版的setTo
更改所有邊緣點。 如果需要繪制較粗的邊緣,可以調用帶有索引-1
drawContours
繪制所有輪廓,而無需for循環。
還請記住,OpenCV Highgui是用於調試目的的男子氣概。 像這樣編程之外的任何事情,都應使用適當的GUI庫(尤其是Qt)來完成。
#include <opencv2/opencv.hpp>
#include <vector>
using namespace std;
using namespace cv;
int main()
{
// Yellow image
Mat3b img = imread("path_to_image");
int step = 5; // Color step
int delay = 30; // Animation speed
bool forward = true;
Scalar color(0,255,255);
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
Mat1b edges;
Canny(gray, edges, 400, 200);
vector<vector<Point>> contours;
findContours(edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
Mat3b canvas(img.rows, img.cols, Vec3b(0,0,0));
while (true)
{
imshow("Draw", canvas);
if (waitKey(delay) == 27 /*ESC*/) break;
// Update color
color[0] = color[0] + ((forward) ? step : -step);
// Deal with direction
if (forward && color[0] > 255) {
forward = false;
color[0] = 255;
}
if (!forward && color[0] < 0) {
forward = true;
color[0] = 0;
}
// Update only edge points
//canvas.setTo(color, edges);
// Draw a thick contour
drawContours(canvas, contours, -1, color, 2);
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.