我是新来OpenCV我试图从目录进行图像处理,使其黑白(灰度),然后将其写入另一个文件。但是输出图像与我所期望的完全不同。也许你可以帮助我,并指出代码中的错误?OpenCV彩色图像灰色故障
#include <iostream>
#include <opencv2/opencv.hpp>
#include <conio.h>
#include <string.h>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdio.h>
#include <stdlib.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
using namespace std;
void faktorial(int InSize, char *DataIn, char *DataOut)// заголовок функции
{
for(int i = 0, j = 0; i < InSize; i += 4, j++)
{
DataOut[j] = (DataIn[i] + DataIn[i + 1] + DataIn[i + 2])/3;
}
}
int main()
{
char* c = "E:\henrik-evensen-castle-valley-v03.jpg";
printf("Input source of image\n Example of right directory file: E:\henrik-evensen-castle-valley-v03.jpg\n Your try:\n");
char *tbLEN;
tbLEN = new char [1024];
cin.getline(tbLEN,1024);
cout << tbLEN;
IplImage* image;
image = cvLoadImage(tbLEN, 1);
int height1 = image->height;
int width1 = image->width;
int step = image->widthStep;
int SizeIn = step*height1;
char* DatIn = image->imageData;
IplImage *image2 = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
char* DatOut = image2->imageData;
faktorial(SizeIn, DatIn, DatOut);
cvNamedWindow("Imagecolor");
cvShowImage("Imagecolor", image);
cvNamedWindow("Gray");
cvShowImage("Gray", image2);
cvWaitKey(0);
return 0;
}
编辑: 我不需要CvtColor功能,我需要使用一个阶乘函数。
是否有任何特定的原因使用C-API而不是C++ API?无论如何,您*都*包括C++头文件,并且C-API据我所知已弃用。除非有人强迫你,否则我会建议不要使用C-API。如果您使用C++ API,您的错误也可能会消失,因为它会为您处理一些事情。 –
[在OpenCV中将RGB转换为黑白]可能的重复(http://stackoverflow.com/questions/1585535/convert-rgb-to-black-white-in-opencv) – Samer
** Samer **很多感谢那个链接,这对我很有用,不知道我怎么能通过这个主题。 ** HannesOvrén**所以IplImage,cvSaveImage,cvCreateImage等都是C-API,对不对?在我的情况下,我需要使用Mat,imwrite和其他来自C++ API的API,对吗? – Generwp