1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| #include <FreeImage.h> #include <opencv2\opencv.hpp> using namespace cv;
#pragma warning(disable : 4996) void FI2MAT(FIBITMAP* src, Mat& dst) {
int bpp = FreeImage_GetBPP(src); FREE_IMAGE_TYPE fit = FreeImage_GetImageType(src);
int cv_type = -1; int cv_cvt = -1;
switch (fit) { case FIT_UINT16: cv_type = DataType<ushort>::type; break; case FIT_INT16: cv_type = DataType<short>::type; break; case FIT_UINT32: cv_type = DataType<unsigned>::type; break; case FIT_INT32: cv_type = DataType<int>::type; break; case FIT_FLOAT: cv_type = DataType<float>::type; break; case FIT_DOUBLE: cv_type = DataType<double>::type; break; case FIT_COMPLEX: cv_type = DataType<Complex<double>>::type; break; case FIT_RGB16: cv_type = DataType<Vec<ushort, 3>>::type; cv_cvt = COLOR_RGB2BGR; break; case FIT_RGBA16: cv_type = DataType<Vec<ushort, 4>>::type; cv_cvt = COLOR_RGBA2BGRA; break; case FIT_RGBF: cv_type = DataType<Vec<float, 3>>::type; cv_cvt = COLOR_RGB2BGR; break; case FIT_RGBAF: cv_type = DataType<Vec<float, 4>>::type; cv_cvt = COLOR_RGBA2BGRA; break; case FIT_BITMAP: switch (bpp) { case 8: cv_type = DataType<Vec<uchar, 1>>::type; break; case 16: cv_type = DataType<Vec<uchar, 2>>::type; break; case 24: cv_type = DataType<Vec<uchar, 3>>::type; break; case 32: cv_type = DataType<Vec<uchar, 4>>::type; break; default: cv_type = -1; } break; default: dst = Mat(); return; }
int width = FreeImage_GetWidth(src); int height = FreeImage_GetHeight(src); int step = FreeImage_GetPitch(src);
if (cv_type >= 0) { dst = Mat(height, width, cv_type, FreeImage_GetBits(src), step); if (cv_cvt > 0) { cvtColor(dst, dst, cv_cvt); } } else {
std::vector<uchar> lut; int n = pow(2, bpp); for (int i = 0; i < n; ++i) { lut.push_back(static_cast<uchar>((255 / (n - 1))*i)); }
FIBITMAP* palletized = FreeImage_ConvertTo8Bits(src); BYTE* data = FreeImage_GetBits(src); for (int r = 0; r < height; ++r) { for (int c = 0; c < width; ++c) { dst.at<uchar>(r, c) = saturate_cast<uchar>(lut[data[r*step + c]]); } } }
flip(dst, dst, 0); }
int main() { FreeImage_Initialise(); std::string imgPath = "F:/cppTry/ger.JPG"; FREE_IMAGE_FORMAT format = FreeImage_GetFileType(imgPath.c_str(), 0); FIBITMAP* fi_image = FreeImage_Load(format, imgPath.c_str());
Mat cvImg2; FI2MAT(fi_image, cvImg2); cvNamedWindow("Image1:",1); cv::imshow("Image1:",cvImg2);
IplImage *img = cvLoadImage(imgPath.c_str()); cvNamedWindow("Image2:",1); cvShowImage("Image2:", img); std::cout << "[free's conv]: type is: " << FreeImage_GetColorType(fi_image) << "\n"; std::cout << "[free's conv]: depth is: " << FreeImage_GetBPP(fi_image) << "\n"; std::cout << "[free's conv]: pitch is: " << FreeImage_GetPitch(fi_image) << "\n"; std::cout << "[cv2 conv]: type is: " << "/" << cvImg2.type() << "\n"; std::cout << "[cv2 conv]: depth is: " << "/" << cvImg2.depth() << "\n"; std::cout << "[cv2 conv]: channel is: " << "/" << cvImg2.channels() << "\n"; std::cout << "[cv2 conv]: elem size is: " << "/" << cvImg2.elemSize() << "\n";
cvWaitKey(); cvDestroyWindow("Image:"); return 0; }
|