感兴趣区域ROI 和 替换算法

#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"highgui.lib")
#pragma comment(lib,"ml.lib")
#pragma comment(lib,"cvcam.lib")
#pragma comment(lib,"cvaux.lib")


#include <stdio.h>
#include <iostream>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

using namespace std;

int main()
{
//头像替换
	
	IplImage* img=cvLoadImage("img//big.jpg");
	IplImage* sub_img=cvLoadImage("img//sub.jpg");

	CvRect rect;
	rect.x=190;
	rect.y=10;
	rect.width=sub_img->width;
	rect.height=sub_img->height;

//自定义算法 实现方式
	for (int y=0;y<sub_img->height;y++)
	{
		unsigned char* r_subimg=(uchar*)(sub_img->imageData + y*sub_img->widthStep);
		unsigned char* r_img=(uchar*)(img->imageData +  (y + rect.y)* img->widthStep);
		for (int x=0;x<sub_img->width;x++)
		{
			//
			r_img[3*(x+rect.x) + 0]=r_subimg[3*x + 0];
			r_img[3*(x+rect.x) + 1]=r_subimg[3*x + 1];
			r_img[3*(x+rect.x) + 2]=r_subimg[3*x + 2];
		}
	}

//利用opencv函数实现
// 	cvSetImageROI(img,rect);//ROI 感兴趣区域
// 	cvCopy(sub_img,img);
// 	cvResetImageROI(img);

	cvNamedWindow("big");
	cvShowImage("big",img);

	
// 	cvNamedWindow("sub");
// 	cvShowImage("sub",sub_img);

	cvWaitKey(0);

	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在车道检测中,我们通常需要提取感兴趣区域(Region of Interest, ROI),以减小计算量并提高检测的准确性。下面是一个简单的例子,演示如何提取图像中的感兴趣区域。 首先,导入必要的库: ```python import cv2 import numpy as np import matplotlib.pyplot as plt %matplotlib inline ``` 然后,读取一张示例图片: ```python img = cv2.imread('example.jpg') ``` 接下来,我们需要定义感兴趣区域的坐标。例如,如果我们想提取图片底部的一部分,可以定义一个梯形区域,如下所示: ```python height, width = img.shape[:2] roi = np.array([[(0, height), (width/2-50, height/2+50), (width/2+50, height/2+50), (width, height)]], dtype=np.int32) ``` 这里使用了NumPy数组,定义了一个梯形区域,顶点分别为`(0, height)`、`(width/2-50, height/2+50)`、`(width/2+50, height/2+50)`和`(width, height)`。这里的`height`和`width`是图像的高度和宽度。 然后,我们需要创建一个与原图像大小相同的黑色图像,用于绘制感兴趣区域: ```python mask = np.zeros_like(img) cv2.fillPoly(mask, roi, (255, 255, 255)) ``` 这里使用了`cv2.fillPoly`函数,将感兴趣区域填充为白色。最后,我们可以通过按位与操作,提取感兴趣区域内的图像: ```python roi_img = cv2.bitwise_and(img, mask) ``` 现在,`roi_img`变量就是提取出的感兴趣区域。我们可以使用`plt.imshow`函数显示它: ```python plt.imshow(cv2.cvtColor(roi_img, cv2.COLOR_BGR2RGB)) ``` 完整代码如下: ```python import cv2 import numpy as np import matplotlib.pyplot as plt %matplotlib inline img = cv2.imread('example.jpg') height, width = img.shape[:2] roi = np.array([[(0, height), (width/2-50, height/2+50), (width/2+50, height/2+50), (width, height)]], dtype=np.int32) mask = np.zeros_like(img) cv2.fillPoly(mask, roi, (255, 255, 255)) roi_img = cv2.bitwise_and(img, mask) plt.imshow(cv2.cvtColor(roi_img, cv2.COLOR_BGR2RGB)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值