我是新來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