[英]OpenCV / Tesseract: How to replace libpng, libtiff etc with GDI+ Bitmap (Load into cv::Mat via GDI+)
我正在開發一個使用OpenCV和Tesseract的項目。 這兩個庫都基於libpng,libtiff,libjpeg等來加載/保存圖像文件。
但是Tesseract(基於Leptonica)使用的這些庫的舊版本具有不兼容的參數。 因此,我不能為兩者使用相同的圖像庫:OpenCV和Tesseract。
因此,如果我動態地編譯項目,則必須在項目中提供大量DLL。 而且,如果我進行靜態編譯,則會產生一個巨大的輸出文件,被炸毀了幾兆字節。
這很丑。 我不要
另一個問題是,幾乎所有主要在Linux / MAC世界中開發的開源項目,如果在Windows上編譯,都不支持Unicode。 在內部,它們都將std::string
傳遞給fopen()
。 在Linux上,使用UTF8編碼路徑的解決方法可能會起作用,但在Windows上則不會。 因此,日語用戶無法在帶有日語名稱的文件夾中打開圖像文件。 盡管Microsoft在1990年代初期就已經做出了巨大努力,將整個Windows NT操作系統轉換為100%Unicode兼容,但是20年后的大多數開源項目(如libpng)仍然不支持通過std::wstring
傳遞路徑。 。
重要說明 :如果要創建支持日語或中文的國際項目,則不得在Windows上使用OpenCV命令imread()
和imwrite()
!
所以,我想要的是:從我的項目中完全消除libtiff,libpng,libjpeg等:
在OpenCV中注釋掉:
// #define HAVE_JASPER
// #define HAVE_JPEG
// #define HAVE_PNG
// #define HAVE_TIFF
etc..
在Tesseract / Leptonica中:
#define HAVE_LIBJPEG 0
#define HAVE_LIBTIFF 0
#define HAVE_LIBPNG 0
#define HAVE_LIBZ 0
#define HAVE_LIBGIF 0
#define HAVE_LIBUNGIF 0
etc..
..,而是使用GDI +,它是Windows操作系統的一部分,並且支持加載/保存BMP,TIF,PNG,JPG,GIF。 此外,GDI +是Unicode兼容的。
我知道這可以用幾行代碼來完成,但是OpenCV項目中缺少這樣一個有用的類。 我的第一個試驗表明,這並不像初看起來那樣瑣碎,因為必須進行許多轉換。
是否有為此目的而制作的課程?
我找不到准備好的課程,所以我寫了自己的一堂課:
我希望它對某人有用,並且希望它作為Windows用戶的可選加載項包含在OpenCV項目中。
好處:
在學習代碼時,您會發現有很多陷阱,並且cv::Mat
和Gdiplus::Bitmap
之間的轉換並不像看起來那樣簡單。
注意:此代碼支持黑白(2位),灰度調色板(8位),24位RGB和32位ARGB圖像。 不支持調色板圖像。 但這並不重要,因為OpenCV也不支持它們,.NET對它們的支持也非常有限。
頭文件:
#pragma once
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
// IMPORTANT:
// This must be included AFTER gdiplus !!
// (OpenCV #undefine's min(), max())
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
class CGdiPlus
{
public:
static void Init();
static Mat ImgRead(const WCHAR* u16_File);
static void ImgWrite(Mat i_Mat, const WCHAR* u16_File);
static Mat CopyBmpToMat(Gdiplus::Bitmap* pi_Bmp);
static Mat CopyBmpDataToMat(Gdiplus::BitmapData* pi_Data);
static Gdiplus::Bitmap* CopyMatToBmp(Mat& i_Mat);
private:
static CLSID GetEncoderClsid(const WCHAR* u16_File);
static BOOL mb_InitDone;
};
CPP文件:
#include "stdafx.h"
#include "CGdiPlus.h"
using namespace Gdiplus;
BOOL CGdiPlus::mb_InitDone = FALSE;
// Do not call this function in the DLL loader lock!
void CGdiPlus::Init()
{
if (mb_InitDone)
return;
GdiplusStartupInput k_Input;
ULONG_PTR u32_Token;
if (Ok != GdiplusStartup(&u32_Token, &k_Input, NULL))
throw L"Error initializing GDI+";
mb_InitDone = TRUE;
}
Mat CGdiPlus::CopyBmpToMat(Bitmap* pi_Bmp)
{
assert(mb_InitDone);
BitmapData i_Data;
Gdiplus::Rect k_Rect(0, 0, pi_Bmp->GetWidth(), pi_Bmp->GetHeight());
if (Ok != pi_Bmp->LockBits(&k_Rect, ImageLockModeRead, pi_Bmp->GetPixelFormat(), &i_Data))
throw L"Error locking Bitmap.";
Mat i_Mat = CopyBmpDataToMat(&i_Data);
pi_Bmp->UnlockBits(&i_Data);
return i_Mat;
}
Mat CGdiPlus::CopyBmpDataToMat(BitmapData* pi_Data)
{
assert(mb_InitDone);
int s32_CvType;
switch (pi_Data->PixelFormat)
{
case PixelFormat1bppIndexed:
case PixelFormat8bppIndexed:
// Special case treated separately below
break;
case PixelFormat24bppRGB: // 24 bit
s32_CvType = CV_8UC3;
break;
case PixelFormat32bppRGB: // 32 bit
case PixelFormat32bppARGB: // 32 bit + Alpha channel
s32_CvType = CV_8UC4;
break;
default:
throw L"Image format not supported.";
}
Mat i_Mat;
if (pi_Data->PixelFormat == PixelFormat1bppIndexed) // 1 bit (special case)
{
i_Mat = Mat(pi_Data->Height, pi_Data->Width, CV_8UC1);
for (UINT Y=0; Y<pi_Data->Height; Y++)
{
BYTE* pu8_Src = (BYTE*)pi_Data->Scan0 + Y * pi_Data->Stride;
BYTE* pu8_Dst = i_Mat.ptr<BYTE>(Y);
BYTE u8_Mask = 0x80;
for (UINT X=0; X<pi_Data->Width; X++)
{
pu8_Dst[0] = (pu8_Src[0] & u8_Mask) ? 255 : 0;
pu8_Dst++;
u8_Mask >>= 1;
if (u8_Mask == 0)
{
pu8_Src++;
u8_Mask = 0x80;
}
}
}
}
else if (pi_Data->PixelFormat == PixelFormat8bppIndexed) // 8 bit gray scale palette (special case)
{
i_Mat = Mat(pi_Data->Height, pi_Data->Width, CV_8UC1);
BYTE* u8_Src = (BYTE*)pi_Data->Scan0;
BYTE* u8_Dst = i_Mat.data;
for (UINT R=0; R<pi_Data->Height; R++)
{
memcpy(u8_Dst, u8_Src, pi_Data->Width);
u8_Src += pi_Data->Stride;
u8_Dst += i_Mat.step;
}
}
else // 24 Bit / 32 Bit
{
// Create a Mat pointing to external memory
Mat i_Ext(pi_Data->Height, pi_Data->Width, s32_CvType, pi_Data->Scan0, pi_Data->Stride);
// Create a Mat with own memory
i_Ext.copyTo(i_Mat);
}
return i_Mat;
}
Bitmap* CGdiPlus::CopyMatToBmp(Mat& i_Mat)
{
assert(mb_InitDone);
PixelFormat e_Format;
switch (i_Mat.channels())
{
case 1: e_Format = PixelFormat8bppIndexed; break;
case 3: e_Format = PixelFormat24bppRGB; break;
case 4: e_Format = PixelFormat32bppARGB; break;
default: throw L"Image format not supported.";
}
// Create Bitmap with own memory
Bitmap* pi_Bmp = new Bitmap(i_Mat.cols, i_Mat.rows, e_Format);
BitmapData i_Data;
Gdiplus::Rect k_Rect(0, 0, i_Mat.cols, i_Mat.rows);
if (Ok != pi_Bmp->LockBits(&k_Rect, ImageLockModeWrite, e_Format, &i_Data))
{
delete pi_Bmp;
throw L"Error locking Bitmap.";
}
if (i_Mat.elemSize1() == 1) // 1 Byte per channel (8 bit gray scale palette)
{
BYTE* u8_Src = i_Mat.data;
BYTE* u8_Dst = (BYTE*)i_Data.Scan0;
int s32_RowLen = i_Mat.cols * i_Mat.channels(); // != i_Mat.step !!
// The Windows Bitmap format requires all rows to be DWORD aligned (always!)
// while OpenCV by default stores bitmap data sequentially.
for (int R=0; R<i_Mat.rows; R++)
{
memcpy(u8_Dst, u8_Src, s32_RowLen);
u8_Src += i_Mat.step; // step may be e.g 3729
u8_Dst += i_Data.Stride; // while Stride is 3732
}
}
else // i_Mat may contain e.g. float data (CV_32F -> 4 Bytes per pixel grayscale)
{
int s32_Type;
switch (i_Mat.channels())
{
case 1: s32_Type = CV_8UC1; break;
case 3: s32_Type = CV_8UC3; break;
default: throw L"Image format not supported.";
}
CvMat i_Dst;
cvInitMatHeader(&i_Dst, i_Mat.rows, i_Mat.cols, s32_Type, i_Data.Scan0, i_Data.Stride);
CvMat i_Img = i_Mat;
cvConvertImage(&i_Img, &i_Dst, 0);
}
pi_Bmp->UnlockBits(&i_Data);
// Add the grayscale palette if required.
if (e_Format == PixelFormat8bppIndexed)
{
CByteArray i_Arr;
i_Arr.SetSize(sizeof(ColorPalette) + 256 * sizeof(ARGB));
ColorPalette* pk_Palette = (ColorPalette*)i_Arr.GetData();
pk_Palette->Count = 256;
pk_Palette->Flags = PaletteFlagsGrayScale;
ARGB* pk_Color = &pk_Palette->Entries[0];
for (int i=0; i<256; i++)
{
pk_Color[i] = Color::MakeARGB(255, i, i, i);
}
if (Ok != pi_Bmp->SetPalette(pk_Palette))
{
delete pi_Bmp;
throw L"Error setting grayscale palette.";
}
}
return pi_Bmp;
}
Mat CGdiPlus::ImgRead(const WCHAR* u16_File)
{
assert(mb_InitDone);
Bitmap i_Bmp(u16_File);
if (!i_Bmp.GetWidth() || !i_Bmp.GetHeight())
throw L"Error loading image from file.";
return CopyBmpToMat(&i_Bmp);
}
void CGdiPlus::ImgWrite(Mat i_Mat, const WCHAR* u16_File)
{
assert(mb_InitDone);
CLSID k_Clsid = GetEncoderClsid(u16_File);
Bitmap* pi_Bmp = CopyMatToBmp(i_Mat);
Status e_Status = pi_Bmp->Save(u16_File, &k_Clsid);
delete pi_Bmp;
if (e_Status != Ok)
throw L"Error saving image to file.";
}
// Get the class identifier of the image encoder for the given file extension.
// e.g. {557CF406-1A04-11D3-9A73-0000F81EF32E} for PNG images
CLSID CGdiPlus::GetEncoderClsid(const WCHAR* u16_File)
{
assert(mb_InitDone);
UINT u32_Encoders, u32_Size;
if (Ok != GetImageEncodersSize(&u32_Encoders, &u32_Size))
throw L"Error obtaining image encoders size";
CByteArray i_Arr;
i_Arr.SetSize(u32_Size);
ImageCodecInfo* pi_Info = (ImageCodecInfo*)i_Arr.GetData();
if (Ok != GetImageEncoders(u32_Encoders, u32_Size, pi_Info))
throw L"Error obtaining image encoders";
CStringW s_Ext = u16_File;
int Pos = s_Ext.ReverseFind('.');
if (Pos < 0)
throw L"Invalid image filename.";
// s_Ext = "*.TIF;"
s_Ext = L"*" + s_Ext.Mid(Pos) + L";";
s_Ext.MakeUpper();
// Search the file extension
for (UINT i=0; i<u32_Encoders; i++)
{
CStringW s_Extensions = pi_Info->FilenameExtension;
s_Extensions += ';';
// s_Extensions = "*.TIFF;*.TIF;"
if (s_Extensions.Find(s_Ext) >= 0)
return pi_Info->Clsid;
pi_Info ++;
}
throw L"No image encoder found for file extension " + s_Ext;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.