cv::rectangle(undistort_color, roi_src, cv::Scalar(100, 100, 100), 2); cv::imshow("undistort color", undistort_color);
时间: 2024-06-02 18:11:35 浏览: 12
这段代码使用OpenCV的函数`cv::rectangle`在图像`undistort_color`上绘制一个矩形框。矩形框的位置和大小由变量`roi_src`指定,颜色为`(100,100,100)`,线宽为2。最后使用`cv::imshow`函数显示绘制好的图像窗口名为"undistort color"。
相关问题
#include <iostream> #include <opencv2/imgcodecs.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/videoio.hpp> #include <opencv2/highgui.hpp> #include <opencv2/video.hpp> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui_c.h> using namespace cv; using namespace std; int main(int argc, char** argv) { VideoCapture capture("D:/dvp/sample/dataset/traffic.mp4"); if (!capture.isOpened()) { //error in opening the video input cerr << "Unable to open file!" << endl; return 0; } Mat frame, roi, hsv_roi, mask; // take first frame of the video capture >> frame; // setup initial location of window Rect track_window(300, 200, 100, 50); // simply hardcoded the values // set up the ROI for tracking roi = frame(track_window); cvtColor(roi, hsv_roi, COLOR_BGR2HSV); inRange(hsv_roi, Scalar(0, 60, 32), Scalar(180, 255, 255), mask); float range_[] = { 0, 180 }; const float* range[] = { range_ }; Mat roi_hist; int histSize[] = { 180 }; int channels[] = { 0 }; calcHist(&hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range); normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX); // Setup the termination criteria, either 10 iteration or move by atleast 1 pt TermCriteria term_crit(TermCriteria::EPS | TermCriteria::COUNT, 10, 1); while (true) { Mat hsv, dst; capture >> frame; if (frame.empty()) break; cvtColor(frame, hsv, COLOR_BGR2HSV); calcBackProject(&hsv, 1, channels, roi_hist, dst, range); // apply meanshift to get the new location meanShift(dst, track_window, term_crit); // Draw it on image rectangle(frame, track_window, 255, 2); imshow("img2", frame); setMouseCallback("img2", onMouse, 0); int keyboard = waitKey(30); if (keyboard == 'q' || keyboard == 27) break; } }帮我更改此段代码,使其能够通过gui使用鼠标来框选指定区域
以下是修改后的代码,可以通过GUI使用鼠标来框选指定区域:
```
#include <iostream>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui_c.h>
using namespace cv;
using namespace std;
Rect box; //定义一个矩形框,用于记录鼠标选择的区域
bool drawing_box = false; //标记是否正在通过鼠标绘制矩形框
void draw_box(Mat& img, Rect box)
{
//绘制矩形框
rectangle(img, box.tl(), box.br(), Scalar(0, 0, 255), 2);
}
void onMouse(int event, int x, int y, int flags, void* param)
{
Mat& image = *(Mat*)param;
switch (event)
{
case EVENT_MOUSEMOVE:
if (drawing_box)
{
//更新矩形框位置
box.width = x - box.x;
box.height = y - box.y;
}
break;
case EVENT_LBUTTONDOWN:
//开始绘制矩形框
drawing_box = true;
box = Rect(x, y, 0, 0);
break;
case EVENT_LBUTTONUP:
//结束绘制矩形框
drawing_box = false;
if (box.width < 0)
{
box.x += box.width;
box.width *= -1;
}
if (box.height < 0)
{
box.y += box.height;
box.height *= -1;
}
draw_box(image, box);
break;
}
}
int main(int argc, char** argv)
{
VideoCapture capture("D:/dvp/sample/dataset/traffic.mp4");
if (!capture.isOpened()) {
//error in opening the video input
cerr << "Unable to open file!" << endl;
return 0;
}
Mat frame, roi, hsv_roi, mask;
// take first frame of the video
capture >> frame;
namedWindow("img2");
setMouseCallback("img2", onMouse, &frame);
while (true) {
capture >> frame;
if (frame.empty()) break;
if (!box.empty()) {
// set up the ROI for tracking
roi = frame(box);
cvtColor(roi, hsv_roi, COLOR_BGR2HSV);
inRange(hsv_roi, Scalar(0, 60, 32), Scalar(180, 255, 255), mask);
float range_[] = { 0, 180 };
const float* range[] = { range_ };
Mat roi_hist;
int histSize[] = { 180 };
int channels[] = { 0 };
calcHist(&hsv_roi, 1, channels, mask, roi_hist, 1, histSize, range);
normalize(roi_hist, roi_hist, 0, 255, NORM_MINMAX);
// Setup the termination criteria, either 10 iteration or move by atleast 1 pt
TermCriteria term_crit(TermCriteria::EPS | TermCriteria::COUNT, 10, 1);
Mat hsv, dst;
cvtColor(frame, hsv, COLOR_BGR2HSV);
calcBackProject(&hsv, 1, channels, roi_hist, dst, range);
// apply meanshift to get the new location
meanShift(dst, box, term_crit);
// Draw it on image
rectangle(frame, box, 255, 2);
}
imshow("img2", frame);
int keyboard = waitKey(30);
if (keyboard == 'q' || keyboard == 27) break;
}
return 0;
}
```
在主函数中,我们添加了一个名为“img2”的窗口,并将其用作鼠标事件的回调函数。在主循环中,我们检查矩形框是否已经被绘制,如果是,则设置ROI并进行跟踪。最后,在每个循环迭代中,我们都显示图像,并等待用户按下“q”或“ESC”键退出程序。
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/SimSun.ttf"); 在PutText函数中指定使用该字体,并将中文字符转换为UTF-8编码。示例代码如下: 复制 Imgproc.putText(image, new String("你好,世界".getBytes("UTF-8")), new Point(100, 100), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 0, 0), 2, Core.LINE_AA, false);提示找不到#include <typeface.h>,添加了也显示找不到
在C++中,`Typeface`类通常用于Android中的Java代码中,因此在C++中不能直接使用该类。如果你需要在C++中使用指定的字体,并将中文字符转换为UTF-8编码,可以使用FreeType库。
以下是一个使用FreeType库在C++中指定字体并绘制中文字符串的示例代码:
```
#include <ft2build.h>
#include FT_FREETYPE_H
// 初始化FreeType库
FT_Library ft;
FT_Init_FreeType(&ft);
// 指定字体文件路径
std::string font_path = "path/to/font.ttf";
// 加载字体文件
FT_Face face;
FT_New_Face(ft, font_path.c_str(), 0, &face);
// 设置字体大小
int font_size = 24;
FT_Set_Pixel_Sizes(face, 0, font_size);
// 设置字符编码为UTF-8
std::string text = "你好,世界";
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring wide_text = converter.from_bytes(text);
// 遍历字符并绘制
for (int i = 0; i < wide_text.length(); i++) {
wchar_t c = wide_text[i];
// 加载字符的位图
FT_Load_Char(face, c, FT_LOAD_RENDER);
// 将位图转换为OpenCV图像
cv::Mat glyph_image(face->glyph->bitmap.rows, face->glyph->bitmap.width, CV_8UC1, face->glyph->bitmap.buffer);
// 在图像上绘制字符
cv::Point position(100 + i * font_size, 100);
cv::rectangle(image, position, position + cv::Point(face->glyph->bitmap.width, face->glyph->bitmap.rows), cv::Scalar(255, 255, 255), -1);
cv::Mat roi = image(cv::Rect(position, position + cv::Point(face->glyph->bitmap.width, face->glyph->bitmap.rows)));
glyph_image.copyTo(roi, face->glyph->bitmap);
}
// 释放资源
FT_Done_Face(face);
FT_Done_FreeType(ft);
```
这段代码使用了FreeType库来加载字体文件并绘制中文字符串。请注意,由于FreeType库是跨平台的,因此这段代码可以在Windows、Linux和Mac OS上运行。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)