修复灰度转黑白算法

This commit is contained in:
gb 2022-09-29 09:34:39 +08:00
parent 8ce837c8ca
commit 5eb5aa60e1
1 changed files with 16 additions and 6 deletions

View File

@ -971,19 +971,29 @@ namespace hg_imgproc
memset(tableData, reverse ? 0 : 1, 256);
memset(tableData, reverse ? 1 : 0, threshold);
cv::Mat dst = cv::Mat::zeros(rows, cols, (int)CV_8UC1);
int* index_dst = new int[image.cols];
int* index_carry = new int[image.cols];
for (size_t i = 0; i < image.cols; i++)
{
index_dst[i] = i / 8;
index_carry[i] = 7 - (i % 8);
}
cv::Mat dst = cv::Mat::zeros(rows, cols, CV_8UC1);
uchar* ptr_src, * ptr_dst;
for (size_t y = 0; y < rows; y++)
{
ptr_dst = dst.ptr<uchar>(y);
ptr_src = const_cast<uchar*>(image.ptr<uchar>(y));
for (size_t x = 0; x < image.cols; x++)
ptr_dst[x / 8] += tableData[ptr_src[x]] << (x % 8);
ptr_dst[index_dst[x]] += tableData[ptr_src[x]] << index_carry[x];
}
delete[] index_dst;
delete[] index_carry;
return dst;
}
// final
public:
int final(void)
@ -1540,9 +1550,9 @@ namespace hg_imgproc
cv::Mat bw = imgproc::convert_8bit_2_1bit(m, threshold, reverse, align);
//std::string ret("");
dst = bw.ptr();
for (int i = 0; i < bw.total(); ++i)
dst[i] = reverse_bit(dst[i]);
//dst = bw.ptr();
//for (int i = 0; i < bw.total(); ++i)
// dst[i] = reverse_bit(dst[i]);
return std::string((char*)bw.ptr(), bw.total());
}