C++ 读取图像中像素的值并保存在OpenCv文件中
我使用一个代码来读取图像(彩色或灰度),转换为灰度,如果它是彩色的,读取每个像素,然后保存到txt文件,但我有一个问题。 当我运行程序时,它会返回以下错误:C++ 读取图像中像素的值并保存在OpenCv文件中,c++,visual-studio,opencv,image-processing,mat,C++,Visual Studio,Opencv,Image Processing,Mat,我使用一个代码来读取图像(彩色或灰度),转换为灰度,如果它是彩色的,读取每个像素,然后保存到txt文件,但我有一个问题。 当我运行程序时,它会返回以下错误: OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\color.cpp, line 3737 问题在哪
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor, file C:\builds\2_4_PackSlave-win32-vc12-shared\opencv\modules\imgproc\src\color.cpp, line 3737
问题在哪里?
我的邮政编码:
#include <opencv2/opencv.hpp>
using namespace cv;
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
Mat colorImage = imread("aust.jpg");
// First convert the image to grayscale.
Mat grayImage;
cvtColor(colorImage, grayImage, CV_RGB2GRAY);
// Then apply thresholding to make it binary.
Mat binaryImage(grayImage.size(), grayImage.type());
// Open the file in write mode.
ofstream outputFile;
outputFile.open("Myfiles.txt");
// Iterate through pixels.
for (int r = 0; r < grayImage.rows; r++)
{
for (int c = 0; c < grayImage.cols; c++)
{
int pixel = grayImage.at<uchar>(r, c);
outputFile << pixel << " ";
}
outputFile << "-1 ";
}
// Close the file.
outputFile.close();
return 0;
}
#包括
使用名称空间cv;
#包括
使用名称空间std;
int main(int argc,字符**argv)
{
Mat colorImage=imread(“aust.jpg”);
//首先将图像转换为灰度。
灰度图像;
CVT颜色(彩色图像、灰度图像、CV_RGB2GRAY);
//然后应用阈值使其二值化。
Mat二进制图像(grayImage.size(),grayImage.type());
//以写模式打开文件。
流输出文件;
outputFile.open(“Myfiles.txt”);
//遍历像素。
对于(int r=0;r outputFile您只需将图像直接加载为灰度,因此无需手动进行转换:
Mat grayImage = imread("aust.jpg", CV_LOAD_IMAGE_GRAYSCALE);
如果要读取彩色图像,然后转换为灰度,则应使用cv\u LOAD\u image\u color
标志调用cv::imread
顺便说一句,评论下面的那行
// Then apply thresholding to make it binary.
不应用阈值。我想您打算使用cv::threshold
您可以直接将图像加载为灰度,因此不需要手动进行转换:
Mat grayImage = imread("aust.jpg", CV_LOAD_IMAGE_GRAYSCALE);
如果要读取彩色图像,然后转换为灰度,则应使用cv\u LOAD\u image\u color
标志调用cv::imread
顺便说一句,评论下面的那行
// Then apply thresholding to make it binary.
不应用阈值。我猜您打算使用cv::threshold
错误意味着cvtColor
的输入图像不是3或4通道图像。请检查通道数:
colorImage.channels();
还要注意的是,您应该使用COLOR\u bgr2 gray
,而不是COLOR\u RGB2GRAY
。OpenCV中的默认顺序是BGR,而不是RGB
COLOR\u XXX2YYY
是新名称,以及以后使用的IMREAD\u XXX
如前所述,您可以直接将图像加载为灰度,使用:
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
要以csv格式存储数据,可以使用cv::format
函数:
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
ofstream out("Myfiles.txt");
out << cv::format(gray, "csv");
Mat1b灰色;
CVT颜色(img,灰色,颜色为灰色);
流输出(“Myfiles.txt”);
out错误意味着cvtColor
的输入图像不是3或4通道图像。请检查通道数:
colorImage.channels();
还要注意的是,您应该使用COLOR\u bgr2 gray
,而不是COLOR\u RGB2GRAY
。OpenCV中的默认顺序是BGR,而不是RGB
COLOR\u XXX2YYY
是新名称,以及以后使用的IMREAD\u XXX
如前所述,您可以直接将图像加载为灰度,使用:
Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
要以csv格式存储数据,可以使用cv::format
函数:
Mat1b gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
ofstream out("Myfiles.txt");
out << cv::format(gray, "csv");
Mat1b灰色;
CVT颜色(img,灰色,颜色为灰色);
流输出(“Myfiles.txt”);
出来