Qt Picture Correction (2)
Sketch
In the previous section, the principle of picture correction is briefly described.This explains the code section.
PDF to Picture Code
PDF to Picture Code <----- Download PDF to Picture Code, click here
//In the PDF to Picture section, I applied a third-party library.Then I encapsulated it myself
//WcharPdfFile: PDF file path;
//imageName: Outgoing Picture Path
//PageNum:PDFThe number of pages
bool CPdf2PngDll::PdfToPng(const wchar_t* wcharPdfFile, const char* imageName, int &pageNum)
{
CMuPDFConvert pdfConvert;
return pdfConvert.Pdf2Png(wcharPdfFile, imageName, pageNum);
}
//Note here that imageName: The output path is a dead png format picture, and passing in parameters to the path of all folders is sufficient, such as output asTest.pngPicture imageName = D:\OfficeWare\test; that's it
bool CMuPDFConvert::Pdf2Png(const wchar_t* wcharPdfFile,const char* imageName, int &pageNum)
{
char tempPath[1024];
strcpy_s(tempPath, imageName);
strcat_s(tempPath, "%d.png");
. . . . . . . . . . . Later, check the source I uploaded, which is omitted.The main purpose is to illustrate the parameter problem.
}
Opencv Picture Correction
Loading the opencv library, which I won't describe here.Here I paste out my correction code.
#include "opencv2/opencv.hpp"
using namespace std;
std::vector<std::vector<cv::Point>> contours;
void CImageRotationDll::ImageRotationStart(const std::string& inputPath, const std::string& outputPath)
{
auto FNsearchQR = [&](cv::Mat image)
{
cv::Mat imageGray, imageROI, imageGuussian, imageSobelX, imageSobelY, imageSobelOut;
//2. Convert to Grayscale
cvtColor(image, imageGray, CV_RGB2GRAY);
imageROI = imageGray(cv::Range(image.rows - 80, image.rows), cv::Range(image.cols - 450, image.cols));
//3. Gauss Smoothing Filter
GaussianBlur(imageROI, imageGuussian, cv::Size(3, 3), 0);
//4. Find the gradient difference between horizontal and vertical gray-scale images using Sobel operator
cv::Mat imageX16S, imageY16S;
Sobel(imageGuussian, imageX16S, CV_16S, 1, 0, 3, 1, 0, 4);
Sobel(imageGuussian, imageY16S, CV_16S, 0, 1, 3, 1, 0, 4);
convertScaleAbs(imageX16S, imageSobelX, 1, 0);//Convert intermediate results to CV_8U
convertScaleAbs(imageY16S, imageSobelY, 1, 0);
imageSobelOut = imageSobelX - imageSobelY;
//5. Mean filter to eliminate high frequency noise
blur(imageSobelOut, imageSobelOut, cv::Size(3, 3));
//6. Binarization
cv::Mat imageSobleOutThreshold;
threshold(imageSobelOut, imageSobleOutThreshold, 180, 255, CV_THRESH_BINARY);
//7. Close operation, fill bar code gap
cv::Mat element = cv::getStructuringElement(0, cv::Size(7, 7));
morphologyEx(imageSobleOutThreshold, imageSobleOutThreshold, cv::MORPH_CLOSE, element);
//8. Corrosion, removing isolated points
erode(imageSobleOutThreshold, imageSobleOutThreshold, element);
//9. Expansion, filling gaps between barcodes, depending on the size of the core, may require 2-3 expansion operations
dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
dilate(imageSobleOutThreshold, imageSobleOutThreshold, element);
//10. Find the rectangular boundary of the barcode area with findContours
vector<cv::Vec4i> hiera;
findContours(imageSobleOutThreshold, contours, hiera, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
};
cv::Mat image;
cv::Mat imagetrans;
image = cv::imread(inputPath);
//Search Barcode
FNsearchQR(image);
int index = 0;
//Determine if barcode is detected or if not rotate 90 degrees clockwise
while (contours.size() != 1)
{
index++;
transpose(image, imagetrans);
flip(imagetrans, image, 1);
if (index == 3)
{
break;
}
FNsearchQR(image);
}
//Preservation
cv::imwrite(outputPath, image);
cv::waitKey();
}
Picture to PDF
#include <QPrinter>
#include <QPainter>
#include <QPageSize>
//Picture to PDF encapsulated library for my QT
void ImageToPdf::startImageToPdf(const QString& imgPath, const QString& pdfpath)
{
QPrinter printer_pixmap(QPrinter::HighResolution);
QPixmap pixmap(imgPath);
printer_pixmap.setOutputFormat(QPrinter::PdfFormat); //Set output format to pdf
printer_pixmap.setOutputFileName(pdfpath); //Set Output Path
printer_pixmap.setPageSize(QPageSize(QSize(pixmap.width(), pixmap.height()))); //Set paper size to A4
QPainter painter_pixmap;
painter_pixmap.begin(&printer_pixmap);
QRect rect = painter_pixmap.viewport();
int mutipleW = rect.width() / pixmap.width();
int mutipleH = rect.height() / pixmap.height();
painter_pixmap.scale(mutipleW, mutipleH);
painter_pixmap.drawPixmap(0, 0, pixmap); //Drawing
painter_pixmap.end();
}
summary
My idea for picture correction is to turn the PDF into a picture, then use opencv to process the picture, and finally, turn the picture into a PDF.Finally, the result I want is that if there are great gods with better ideas and algorithms, don't forget to mention them.Thank you here.
Just for recording, just for sharing! May what you write help you.