我想创建一个矩阵数组来分割图像。
以下是我为创建数组所做的工作,它显示了" EXC_I386_GPFLT"的错误。
我该如何解决它或我该怎样做才能实现我的目的?
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( )
{
Mat img;
img = imread("/Users/koike1979/Documents/0306/trucka.bmp", CV_LOAD_IMAGE_COLOR);
namedWindow( "Original image", CV_WINDOW_AUTOSIZE );
imshow( "Original image", img );
Mat H[2]= {Mat(20,20,CV_8UC1),Mat(20,20,CV_8UC1)};
for (int i=0; i<200; i++)
for (int j=0; j<200; j++)
{
Vec3b intensity2 = img.at<Vec3b>(i ,j);
int blue = intensity2.val[0];
int green = intensity2.val[1];
int red = intensity2.val[2];
H[0].at<uchar>(i,j)=(blue+green+red)/3;
}
namedWindow( "Modify pixel", CV_WINDOW_AUTOSIZE );
imshow( "Modify pixel", H[0] );
waitKey(0);
return 0;
}
答案 0 :(得分:1)
https://stackoverflow.com/a/19652248/4518710
创建矩阵数组的主要目的是什么?
您的代码似乎有一些问题,
首先,您需要通过硬编码访问Mat NOT的坐标
请改用Mat :: rows和Mat :: cols
原:
for (int i=0; i<200; i++)
for (int j=0; j<200; j++)
{
}
改性:
int rows = img.rows;
if(rows > 200)
rows = 200;
int cols = img.cols;
if(cols > 200)
cols = 200;
for (int i=0; i<rows; i++)
for (int j=0; j<cols; j++)
{
}
其次,使用向量&lt;&gt; C ++的特性而不是数组本身
原:
Mat H[2]= {Mat(20,20,CV_8UC1),Mat(20,20,CV_8UC1)};
改性:
std::vector<Mat> H;
H.push_back(Mat(20, 20, CV_8UC1));
H.push_back(Mat(20, 20, CV_8UC1));
第三,直接将int转换为uchar可能会产生潜在的副作用
请改用static_cast
原:
H[0].at<uchar>(i,j)=(blue+green+red)/3;
改性:
H[0].at<uchar>(i,j) = static_cast<uchar>((blue+green+red)/3);
答案 1 :(得分:0)
您可以使用std :: vector
Mat img;
img = imread("./res/mydhm.png", CV_LOAD_IMAGE_COLOR);
namedWindow("Original image", CV_WINDOW_AUTOSIZE);
imshow("Original image", img);
//Mat H[2] = { Mat(img.cols, img.row, img.type), Mat(img.cols, img.row, img.type) };
std::vector<Mat> H; // use vector
H.push_back(Mat(img.cols, img.rows, img.type()));
H.push_back(Mat(img.cols, img.rows, img.type()));
for (int i = 0; i<img.cols; i++)
{
for (int j = 0; j<img.rows; j++)
{
Vec3b intensity2 = img.at<Vec3b>(Point(i, j));
int blue = intensity2.val[0];
int green = intensity2.val[1];
int red = intensity2.val[2];
H[0].at<Vec3b>(Point(i, j)) = (uchar)(blue + green + red) / 3;
}
}
namedWindow("Modify pixel", CV_WINDOW_AUTOSIZE);
imshow("Modify pixel", H[0]);
waitKey(0);
不要忘记#include<vector>