【发布时间】:2016-04-20 14:46:33
【问题描述】:
我是 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,您的错误也可能会消失,因为它会为您处理一些事情。
-
Samer 非常感谢那个链接,它对我很有用,不知道我怎么能通过那个主题。 Hannes Ovrén 所以 IplImage、cvSaveImage、cvCreateImage 等都是 C-API,对吧?就我而言,我需要使用来自 C++ API 的 Mat、imwrite 和其他工具,对吗?
-
检查this。只需将像素相加并除以 3,而不是对它们进行 ORing。请记住在对像素求和之前转换为 int,否则您会饱和。另外,忘记过时的 C api,使用 C++ api
-
作为@Miki 评论的补充,请查看this page