level 2
DYBALA😳
楼主
代码如下 得不到结果 新手一个 求大神解答
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <opencv\cv.h>
void Image_Minus(IplImage *X, IplImage *Y, IplImage *X_Y)
{
//图像差分函数,将图像1中像素和图像2中对应像素想减,要求X、Y、X_Y大小相同
int i, j, width, height, step, chanel;
unsigned char *dataX, *dataY, *dataX_Y;
width = X->width;
height = X->height;
dataX = (unsigned char *)X->imageData;
dataY = (unsigned char *)Y->imageData;
dataX_Y = (unsigned char *)X_Y->imageData;
step = X->widthStep / sizeof(char);
chanel = X->nChannels;
for (i = 0; i<height; i++)
for (j = 0; j<width*chanel; j++)
dataX_Y[i*step + j] = abs(dataX[i*step + j] - dataY[i*step + j]);
}
int main()
{
IplImage* pImgX;
IplImage* pImgY;
IplImage* pImgX_Y;
CvSize dest_size;
pImgX = cvLoadImage("2.jpg", -1);
pImgY = cvLoadImage("3.jpg", -1);
if (pImgX == 0 || pImgY == 0)
{
printf("载入文件失败!/n");
return -1;
}
dest_size.width = pImgX->width;
dest_size.height = pImgX->height;
pImgX_Y = cvCreateImage(dest_size, pImgX->depth, pImgX->nChannels);
//图像差分
Image_Minus(pImgX, pImgY, pImgX_Y);
//创建窗口
cvNamedWindow("Picture X:", 1);
cvNamedWindow("Picture Y:", 1);
cvNamedWindow("Picture X-Y:", 1);
//显示图像
cvShowImage("Picture X:", pImgX);
cvShowImage("Picture Y:", pImgY);
cvShowImage("Picture X-Y:", pImgX_Y);
cvWaitKey(0);
//销毁窗口
cvDestroyWindow("Picture X:");
cvDestroyWindow("Picture Y:");
cvDestroyWindow("Picture X-Y:");
//释放图像
cvReleaseImage(&pImgX);
cvReleaseImage(&pImgY);
cvReleaseImage(&pImgX_Y);
return 0;
}
IplImage* img = cvLoadImage("Picture X-Y", 1);
Mat cvMatImg(img);
void salt(Mat &image, int n)
{
for (int k = 0; k < n; k++)
{
rand();
int i = rand() % image.cols;
int j = rand() % image.rows;
if (image.channels() == 1)
{
image.at<uchar>(j, i) = 255;
}
else if (image.channels() == 3)
{
image.at<Vec3b>(j, i)[0] = 255;
image.at<Vec3b>(j, i)[1] = 255;
image.at<Vec3b>(j, i)[2] = 255;
}
}
cout << "一行含有的字节数:" << image.step << endl;
cout << "一个像素所含有的字节:" << image.elemSize() << endl;
cout << "通道数:" << image.channels() << endl;
cout << "总像素个数:" << image.total() << endl;
cout << "width:" << image.size().width << endl;
cout << "width:" << image.cols << endl;
cout << "height:" << image.rows << endl;
}
void salt2(Mat_<Vec3b> image, int n) // 这个只针对彩色三通道的图片
{
for (int k = 0; k < n; k++)
{
rand();
int i = rand() % image.cols;
int j = rand() % image.rows;
if (image.channels() == 3)
{
image(j, i)[0] = 255;
image(j, i)[1] = 255;
image(j, i)[2] = 255;
}
}
}
int main()
{
Mat image = imread("1.jpg", 1);
salt(image, 3000);
namedWindow("image");
imshow("image", image);
waitKey(0);
return 0;
}
2017年05月11日 09点05分
1
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2\core\core.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <opencv\cv.h>
void Image_Minus(IplImage *X, IplImage *Y, IplImage *X_Y)
{
//图像差分函数,将图像1中像素和图像2中对应像素想减,要求X、Y、X_Y大小相同
int i, j, width, height, step, chanel;
unsigned char *dataX, *dataY, *dataX_Y;
width = X->width;
height = X->height;
dataX = (unsigned char *)X->imageData;
dataY = (unsigned char *)Y->imageData;
dataX_Y = (unsigned char *)X_Y->imageData;
step = X->widthStep / sizeof(char);
chanel = X->nChannels;
for (i = 0; i<height; i++)
for (j = 0; j<width*chanel; j++)
dataX_Y[i*step + j] = abs(dataX[i*step + j] - dataY[i*step + j]);
}
int main()
{
IplImage* pImgX;
IplImage* pImgY;
IplImage* pImgX_Y;
CvSize dest_size;
pImgX = cvLoadImage("2.jpg", -1);
pImgY = cvLoadImage("3.jpg", -1);
if (pImgX == 0 || pImgY == 0)
{
printf("载入文件失败!/n");
return -1;
}
dest_size.width = pImgX->width;
dest_size.height = pImgX->height;
pImgX_Y = cvCreateImage(dest_size, pImgX->depth, pImgX->nChannels);
//图像差分
Image_Minus(pImgX, pImgY, pImgX_Y);
//创建窗口
cvNamedWindow("Picture X:", 1);
cvNamedWindow("Picture Y:", 1);
cvNamedWindow("Picture X-Y:", 1);
//显示图像
cvShowImage("Picture X:", pImgX);
cvShowImage("Picture Y:", pImgY);
cvShowImage("Picture X-Y:", pImgX_Y);
cvWaitKey(0);
//销毁窗口
cvDestroyWindow("Picture X:");
cvDestroyWindow("Picture Y:");
cvDestroyWindow("Picture X-Y:");
//释放图像
cvReleaseImage(&pImgX);
cvReleaseImage(&pImgY);
cvReleaseImage(&pImgX_Y);
return 0;
}
IplImage* img = cvLoadImage("Picture X-Y", 1);
Mat cvMatImg(img);
void salt(Mat &image, int n)
{
for (int k = 0; k < n; k++)
{
rand();
int i = rand() % image.cols;
int j = rand() % image.rows;
if (image.channels() == 1)
{
image.at<uchar>(j, i) = 255;
}
else if (image.channels() == 3)
{
image.at<Vec3b>(j, i)[0] = 255;
image.at<Vec3b>(j, i)[1] = 255;
image.at<Vec3b>(j, i)[2] = 255;
}
}
cout << "一行含有的字节数:" << image.step << endl;
cout << "一个像素所含有的字节:" << image.elemSize() << endl;
cout << "通道数:" << image.channels() << endl;
cout << "总像素个数:" << image.total() << endl;
cout << "width:" << image.size().width << endl;
cout << "width:" << image.cols << endl;
cout << "height:" << image.rows << endl;
}
void salt2(Mat_<Vec3b> image, int n) // 这个只针对彩色三通道的图片
{
for (int k = 0; k < n; k++)
{
rand();
int i = rand() % image.cols;
int j = rand() % image.rows;
if (image.channels() == 3)
{
image(j, i)[0] = 255;
image(j, i)[1] = 255;
image(j, i)[2] = 255;
}
}
}
int main()
{
Mat image = imread("1.jpg", 1);
salt(image, 3000);
namedWindow("image");
imshow("image", image);
waitKey(0);
return 0;
}