Tesseract 似乎不能很好地处理位图,它可以处理某些输入但会搞砸其他输入。同时在与之前相同但采用 leptonica 格式 PIX 的输入上表现良好。
如何将内存中的位图转换为 PIX?
想到的一个想法是使用 leptonica 的 pixReadMem() :
00724 /*---------------------------------------------------------------------*
00725 * Read from memory *
00726 *---------------------------------------------------------------------*/
00727 /*!
00728 * pixReadMem()
00729 *
00730 * Input: data (const; encoded)
00731 * datasize (size of data)
00732 * Return: pix, or null on error
00733 *
00734 * Notes:
00735 * (1) This is a variation of pixReadStream(), where the data is read
00736 * from a memory buffer rather than a file.
00737 * (2) On windows, this will only read tiff formatted files from
00738 * memory. For other formats, it requires fmemopen(3).
00739 * Attempts to read those formats will fail at runtime.
00740 * (3) findFileFormatBuffer() requires up to 8 bytes to decide on
00741 * the format. That determines the constraint here.
00742 */
所以现在我需要找到一种方法来从内存中的位图在内存中构造一个 TIFF。但我不知道怎么办。
最佳答案
好吧,我决定自己用一个快速的脏端口来做这件事。 我只是使用文件加载 BMP 函数并创建了假的 fread/fseek,因为我真的很懒。 有效。
如果有人需要,我会贴在下面。 令人困惑的是,为什么 leptonica 开发人员没有为 Windows 正确地做到这一点。
#include <cstring>
#include <leptonica/allheaders.h>
#include "leptonica_hack.h"
#include "bmp.h"
int fake_file_tracker = 0;
size_t fake_fread ( void * ptr, size_t size, size_t count, char * buffer, size_t buflen)
{
if (fake_file_tracker >= buflen)
{
return 0;
}
if (fake_file_tracker + size * count > buflen)
{
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), (size_t)(buflen - fake_file_tracker));
fake_file_tracker = buflen+1;
return (size_t)((buflen - fake_file_tracker) / size);
}
(void)memcpy(ptr, (void *)(buffer+fake_file_tracker), size * count);
fake_file_tracker += size * count;
return count;
}
int fake_fseek ( char * buffer, long int offset, int origin, size_t buflen)
{
if (origin + offset >= buflen)
return 0;
fake_file_tracker = origin + offset;
return 0;
}
/*!
* pixReadStreamBmp()
*
* Input: stream opened for read
* Return: pix, or null on error
*
* Notes:
* (1) Here are references on the bmp file format:
* http://en.wikipedia.org/wiki/BMP_file_format
* http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
*/
PIX *
pixReadBmpFromBuffer(char *fp, size_t buflen)
{
fake_file_tracker = 0;
l_uint16 sval;
l_uint32 ival;
l_int16 bfType, bfSize, bfFill1, bfReserved1, bfReserved2;
l_int16 offset, bfFill2, biPlanes, depth, d;
l_int32 biSize, width, height, xres, yres, compression, ignore;
l_int32 imagebytes, biClrUsed, biClrImportant;
l_uint8 *colormapBuf = NULL;
l_int32 colormapEntries;
l_int32 fileBpl, extrabytes, readerror;
l_int32 pixWpl, pixBpl;
l_int32 i, j, k;
l_uint8 pel[4];
l_uint8 *data;
l_uint32 *line, *pword;
PIX *pix, *pixt;
PIXCMAP *cmap;
PROCNAME("pixReadBmpFromBuffer");
if (!fp)
return (PIX *)ERROR_PTR("fp not defined", procName, NULL);
/* Read bitmap file header */
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfType = convertOnBigEnd16(sval);
if (bfType != BMP_ID)
return (PIX *)ERROR_PTR("not bmf format", procName, NULL);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfSize = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved1 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfReserved2 = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
offset = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
bfFill2 = convertOnBigEnd16(sval);
/* Read bitmap info header */
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biSize = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
width = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
height = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
biPlanes = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&sval, 1, 2, fp, buflen);
depth = convertOnBigEnd16(sval);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
compression = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
imagebytes = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
xres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
yres = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrUsed = convertOnBigEnd32(ival);
ignore = fake_fread((char *)&ival, 1, 4, fp, buflen);
biClrImportant = convertOnBigEnd32(ival);
if (compression != 0)
return (PIX *)ERROR_PTR("cannot read compressed BMP files",
procName,NULL);
/* A little sanity checking. It would be nice to check
* if the number of bytes in the file equals the offset to
* the data plus the imagedata, but this won't work when
* reading from memory, because fmemopen() doesn't implement
* ftell(). So we can't do that check. The imagebytes for
* uncompressed images is either 0 or the size of the file data.
* (The fact that it can be 0 is perhaps some legacy glitch). */
if (width < 1)
return (PIX *)ERROR_PTR("width < 1", procName,NULL);
if (height < 1)
return (PIX *)ERROR_PTR("height < 1", procName,NULL);
if (depth < 1 || depth > 32)
return (PIX *)ERROR_PTR("depth not in [1 ... 32]", procName,NULL);
fileBpl = 4 * ((width * depth + 31)/32);
if (imagebytes != 0 && imagebytes != fileBpl * height)
return (PIX *)ERROR_PTR("invalid imagebytes", procName,NULL);
if (offset < BMP_FHBYTES + BMP_IHBYTES)
return (PIX *)ERROR_PTR("invalid offset: too small", procName,NULL);
if (offset > BMP_FHBYTES + BMP_IHBYTES + 4 * 256)
return (PIX *)ERROR_PTR("invalid offset: too large", procName,NULL);
/* Handle the colormap */
colormapEntries = (offset - BMP_FHBYTES - BMP_IHBYTES) / sizeof(RGBA_QUAD);
if (colormapEntries > 0) {
if ((colormapBuf = (l_uint8 *)CALLOC(colormapEntries,
sizeof(RGBA_QUAD))) == NULL)
return (PIX *)ERROR_PTR("colormapBuf alloc fail", procName, NULL );
/* Read colormap */
if (fake_fread(colormapBuf, sizeof(RGBA_QUAD), colormapEntries, fp, buflen)
!= colormapEntries) {
FREE(colormapBuf);
return (PIX *)ERROR_PTR( "colormap read fail", procName, NULL);
}
}
/* Make a 32 bpp pix if depth is 24 bpp */
d = depth;
if (depth == 24)
d = 32;
if ((pix = pixCreate(width, height, d)) == NULL)
return (PIX *)ERROR_PTR( "pix not made", procName, NULL);
pixSetXRes(pix, (l_int32)((l_float32)xres / 39.37 + 0.5)); /* to ppi */
pixSetYRes(pix, (l_int32)((l_float32)yres / 39.37 + 0.5)); /* to ppi */
pixWpl = pixGetWpl(pix);
pixBpl = 4 * pixWpl;
cmap = NULL;
if (colormapEntries > 256)
L_WARNING("more than 256 colormap entries!", procName);
if (colormapEntries > 0) { /* import the colormap to the pix cmap */
cmap = pixcmapCreate(L_MIN(d, 8));
FREE(cmap->array); /* remove generated cmap array */
cmap->array = (void *)colormapBuf; /* and replace */
cmap->n = L_MIN(colormapEntries, 256);
}
pixSetColormap(pix, cmap);
/* Seek to the start of the bitmap in the file */
fake_fseek(fp, offset, 0, buflen);
if (depth != 24) { /* typ. 1 or 8 bpp */
data = (l_uint8 *)pixGetData(pix) + pixBpl * (height - 1);
for (i = 0; i < height; i++) {
if (fake_fread(data, 1, fileBpl, fp, buflen) != fileBpl) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
data -= pixBpl;
}
}
else { /* 24 bpp file; 32 bpp pix
* Note: for bmp files, pel[0] is blue, pel[1] is green,
* and pel[2] is red. This is opposite to the storage
* in the pix, which puts the red pixel in the 0 byte,
* the green in the 1 byte and the blue in the 2 byte.
* Note also that all words are endian flipped after
* assignment on L_LITTLE_ENDIAN platforms.
*
* We can then make these assignments for little endians:
* SET_DATA_BYTE(pword, 1, pel[0]); blue
* SET_DATA_BYTE(pword, 2, pel[1]); green
* SET_DATA_BYTE(pword, 3, pel[2]); red
* This looks like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
* and after byte flipping:
* 3 2 (B) 1 (G) 0 (R)
* |-----------|------------|-----------|-----------|
*
* For big endians we set:
* SET_DATA_BYTE(pword, 2, pel[0]); blue
* SET_DATA_BYTE(pword, 1, pel[1]); green
* SET_DATA_BYTE(pword, 0, pel[2]); red
* This looks like:
* 0 (R) 1 (G) 2 (B) 3
* |-----------|------------|-----------|-----------|
* so in both cases we get the correct assignment in the PIX.
*
* Can we do a platform-independent assignment?
* Yes, set the bytes without using macros:
* *((l_uint8 *)pword) = pel[2]; red
* *((l_uint8 *)pword + 1) = pel[1]; green
* *((l_uint8 *)pword + 2) = pel[0]; blue
* For little endians, before flipping, this looks again like:
* 3 (R) 2 (G) 1 (B) 0
* |-----------|------------|-----------|-----------|
*/
readerror = 0;
extrabytes = fileBpl - 3 * width;
line = pixGetData(pix) + pixWpl * (height - 1);
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
pword = line + j;
if (fake_fread(&pel, 1, 3, fp, buflen) != 3)
readerror = 1;
*((l_uint8 *)pword + COLOR_RED) = pel[2];
*((l_uint8 *)pword + COLOR_GREEN) = pel[1];
*((l_uint8 *)pword + COLOR_BLUE) = pel[0];
}
if (extrabytes) {
for (k = 0; k < extrabytes; k++)
ignore = fake_fread(&pel, 1, 1, fp, buflen);
}
line -= pixWpl;
}
if (readerror) {
pixDestroy(&pix);
return (PIX *)ERROR_PTR("BMP read fail", procName, NULL);
}
}
pixEndianByteSwap(pix);
/* ----------------------------------------------
* The bmp colormap determines the values of black
* and white pixels for binary in the following way:
* if black = 1 (255), white = 0
* 255, 255, 255, 0, 0, 0, 0, 0
* if black = 0, white = 1 (255)
* 0, 0, 0, 0, 255, 255, 255, 0
* We have no need for a 1 bpp pix with a colormap!
* ---------------------------------------------- */
if (depth == 1 && cmap) {
/* L_INFO("Removing colormap", procName); */
pixt = pixRemoveColormap(pix, REMOVE_CMAP_BASED_ON_SRC);
pixDestroy(&pix);
pix = pixt; /* rename */
}
return pix;
}
关于c++ - 如何将位图转换为内存中的 PIX?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947929/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h