<h1 style="font-size: 32px; border-bottom: 2px solid rgb(204, 204, 204); padding: 0px 4px 0px 0px; margin: 0px 0px 10px;">ROI矩形截取</h1><p><span style=" color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif;">当前系列所有demo下载地址:</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); text-wrap: wrap; box-sizing: border-box;"><a href="https://github.com/GaoRenBao/OpenCv4-Demo" target="_blank" style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(0, 102, 0); transition-duration: 0.2s; transition-property: opacity; outline: none; opacity: 0.8;">https://github.com/GaoRenBao/OpenCv4-Demo</a></p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; box-sizing: border-box; color: rgb(51, 51, 51);">不同编程语言对应的OpenCv版本以及<span style="">开发环境</span>信息如下:<span style="font-family: Calibri; font-size: 14px;"> </span></p><table border="1" style="text-wrap: wrap; border-right: none; border-bottom: none; border-image: initial; border-left: 1px solid rgb(102, 102, 102); border-top: 1px solid rgb(102, 102, 102);"><tbody><tr class="firstRow"><td width="81" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;">语言</span></strong></p></td><td width="223" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv</span>版本</span></strong></p></td><td width="242" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">IDE</span></span></strong></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C#</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: Calibri; letter-spacing: 0px;">OpenCvSharp4.4.8.0.20230708</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C++</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">O</span></span><span style="font-family: Calibri; letter-spacing: 0px;">pen</span><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">C</span></span><span style="font-family: Calibri; letter-spacing: 0px;">v-4.5.5-vc14_vc15</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Python</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv-Python (4.6.0.66)</span></span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">PyCharm Community Edition 2022.1.3</span></span></p></td></tr></tbody></table><p><br/></p><p style="margin-top: 0px; margin-bottom: 10px; box-sizing: border-box; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); text-wrap: wrap; ">下面给大家演示了C#、C++、Python这三种环境下是如何截取图像的ROI区域。</p><p>测试原图如下:<br/></p><p><img src="/upload/image/6383425833982080532781706.jpg" title="messi5.jpg" alt="messi5.jpg"/></p><p>裁剪效果如下:</p><p><img src="/upload/image/6383425839809805937906446.jpg" title="roi.jpg" alt="roi.jpg"/></p><p><br/></p><p style="text-wrap: wrap;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">C#版本代码如下:</span></strong></p><pre class="brush:c#;toolbar:false">using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img = Cv2.ImRead("../../../images/messi5.jpg");
// 设置需要裁剪的轮廓
Rect rect = new Rect()
{
X = 100,
Y = 100,
Width = 200,
Height = 200
};
// 裁剪
Mat roi = new Mat(img, rect);
Cv2.ImShow("img", img);
Cv2.ImShow("roi.jpg", roi);
Cv2.WaitKey(0);
}
}
}</pre><p><br/></p><p><strong style="text-wrap: wrap; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">C++版本代码如下:</span></strong></strong></p><pre class="brush:cpp;toolbar:false">#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = cv::imread("../images/messi5.jpg");
// 设置需要裁剪的轮廓
Rect rect;
rect.x = 100;
rect.y = 100;
rect.width = 200;
rect.height = 200;
// 裁剪
Mat roi = img(rect);
cv::imshow("img", img);
cv::imshow("roi.jpg", roi);
cv::waitKey(0);
}</pre><p><br/></p><p><strong style="text-wrap: wrap; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">Python版本代码如下:</span></strong></strong></p><pre class="brush:python;toolbar:false">import cv2
img = cv2.imread('../images/messi5.jpg')
# 设置需要裁剪的轮廓
x = 100
y = 100
w = 200
h = 200
# 裁剪
roi = img[y:y + h, x:x + w].copy()
cv2.imshow('img', img)
cv2.imshow('roi.jpg', roi)
cv2.waitKey(0)
cv2.destroyAllWindows()</pre><p><br/></p>
ROI矩形截取 当前系列所有demo下载地址:
https://github.com/GaoRenBao/OpenCv4-Demo
不同编程语言对应的OpenCv版本以及开发环境 信息如下:
语言
OpenCv 版本
IDE
C#
OpenCvSharp4.4.8.0.20230708
Visual Studio 2022
C++
O pen C v-4.5.5-vc14_vc15
Visual Studio 2022
Python
OpenCv-Python (4.6.0.66)
PyCharm Community Edition 2022.1.3
下面给大家演示了C#、C++、Python这三种环境下是如何截取图像的ROI区域。
测试原图如下:
裁剪效果如下:
C#版本代码如下:
using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img = Cv2.ImRead("../../../images/messi5.jpg");
// 设置需要裁剪的轮廓
Rect rect = new Rect()
{
X = 100,
Y = 100,
Width = 200,
Height = 200
};
// 裁剪
Mat roi = new Mat(img, rect);
Cv2.ImShow("img", img);
Cv2.ImShow("roi.jpg", roi);
Cv2.WaitKey(0);
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img = cv::imread("../images/messi5.jpg");
// 设置需要裁剪的轮廓
Rect rect;
rect.x = 100;
rect.y = 100;
rect.width = 200;
rect.height = 200;
// 裁剪
Mat roi = img(rect);
cv::imshow("img", img);
cv::imshow("roi.jpg", roi);
cv::waitKey(0);
}
Python版本代码如下:
import cv2
img = cv2.imread('../images/messi5.jpg')
# 设置需要裁剪的轮廓
x = 100
y = 100
w = 200
h = 200
# 裁剪
roi = img[y:y + h, x:x + w].copy()
cv2.imshow('img', img)
cv2.imshow('roi.jpg', roi)
cv2.waitKey(0)
cv2.destroyAllWindows()