显示像素值已更改的图像openCV
Showing image with changed pixel values openCV
我是openCV和C++的新手。我想更改我加载的图像的像素值,并在另一个窗口中显示新图像,以比较结果(只是视觉上)。然而,当我运行代码时,我会得到两个原始图像。这意味着要么我的for循环没有做它应该做的事情(我对此表示怀疑,因为这对我来说是有意义的),要么像素值丢失,无法保存以显示新图像。我读过之前的一篇帖子,说我应该在处理每个像素以设置到修改后的图像后包含这一声明。语句为:img.at(Point(x,y))=color。
有人能告诉我我做错了什么吗?
谢谢
cv::Mat img = cv::imread("12.jpg", CV_LOAD_IMAGE_COLOR);
// start of pixel navigation
Mat navigateImage(Mat) {
for(int x = 0; x > img.rows; x++)
{
for(int y = 0; y > img.cols; y++){
Vec3b color = img.at<Vec3b>(Point(x,y));
if ( color[0] > 10 && color [1] > 10 && color[2]> 10 )
{
color[0] = 0 ;
color[1] = 0;
color[2] = 0;
img.at<Vec3b>(Point(x,y)) = color;
}
else
{
color.val[0] = 255 ;
color.val[1] = 255;
color.val[2] = 255;
img.at<Vec3b>(Point(x,y)) = color;
}
}
}
return img;
}
// end of pixel navigation
int main( int argc, char** argv )
{
if(! img.data){
cout << "could not open or find the image" << endl;
return -1;}
Mat newImage = navigateImage(img);
cv::imshow( " Original", img);
cv::imshow( " Altered ", newImage);
cv::waitKey(0);
return 0;
}
(1)。首先,
for(int x = 0; x > img.rows; x++)
和
for(int y = 0; y > img.cols; y++)
应该是
for(int x = 0; x < img.cols; x++)
和
for(int y = 0; y < img.rows; y++)
分别。
因为,你永远不会因为这个错误而进入循环,所以两个图像是相同的。
(2)。其次,
Mat navigateImage(Mat)
应该是
Mat navigateImage(Mat img)
(3)。第三,投放
cv::Mat img = cv::imread("12.jpg", CV_LOAD_IMAGE_COLOR);
在CCD_ 1函数中。
(4)。最后一点更换,
Mat newImage = navigateImage();
通过
Mat newImage = navigateImage(img.clone());
否则,两个图像将相同。
更正代码-
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
// start of pixel navigation
Mat navigateImage(Mat img) {
for(int x = 0; x < img.cols; x++)
{
for(int y = 0; y < img.rows; y++){
Vec3b color = img.at<Vec3b>(Point(x,y));
if ( color[0] > 10 && color [1] > 10 && color[2]> 10 )
{
color[0] = 0 ;
color[1] = 0;
color[2] = 0;
img.at<Vec3b>(Point(x,y)) = color;
}
else
{
color.val[0] = 255 ;
color.val[1] = 255;
color.val[2] = 255;
img.at<Vec3b>(Point(x,y)) = color;
}
}
}
return img;
}
// end of pixel navigation
int main( int argc, char** argv )
{
Mat img = cv::imread("12.png", CV_LOAD_IMAGE_COLOR);
if(! img.data){
cout << "could not open or find the image" << endl;
return -1;
}
Mat newImage = navigateImage(img.clone());
cv::imshow( " Original", img);
cv::imshow( " Altered ", newImage);
cv::waitKey(0);
return 0;
}
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如果不在窗口 10 中声明名为 openCV 的 openCV namedWindow,QT 图像显示将无法正常工作
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 如何在 OpenCV 中将图像与另一个图像相关联
- 如何在 OpenCV c++ 中压缩 TIFF 格式的图像?
- OpenCV - 如何在灰度图像上应用 Kmeans?
- 阅读和处理图像(OpenCV/C )
- cv::findContours 正在修改源图像 OpenCV 2.3
- 将图像替换为更大尺寸的图像 (OpenCV)
- 显示像素值已更改的图像openCV
- 具有积分图像的快速模糊图像(OpenCV)
- 扫描图像OpenCV上的文档检测
- 使用cv::Mat图像(opencv)如何检测对象
- 写作和展示图像OpenCV
- 从文件夹中读取图像(OpenCV)
- 无法保存从网络摄像头捕获的图像(OpenCV 2.3 的 imwrite 编译错误)
- 旋转不变的多个模板出现在图像Opencv