#include "iostream"
#include "cv.h"
#include "highgui.h"
#include "cvaux.h"
#include "cxmisc.h"
#include "math.h"
using namespace cv;
using namespace std;
int main(){
int height, width, x, y, i, minX, minY, maxX, maxY;
char imgFileName[100];
IplImage *origImage = cvLoadImage("data set baybayin/0000.jpg", -1);
height = origImage->height;
width = origImage->width;
IplImage *grayImage = cvCreateImage(cvSize(width, height), 8, 1);
IplImage *binImage = cvCreateImage(cvSize(width, height), 8, 1);
//Pre-processing phase
//image cleaning
cvCvtColor(origImage, grayImage, CV_BGR2GRAY);
cvDilate(grayImage, grayImage, NULL, 1);
cvSmooth(grayImage, grayImage, CV_GAUSSIAN, 21, 21, 0, 0);
//image binarization
cvThreshold(grayImage, binImage, 120, 255, CV_THRESH_BINARY);
//cvNormalize(binImage,binImage,0,1,CV_MINMAX);
//getting image info
minX = width;
minY = height;
maxX = 0;
maxY = 0;
//image cropping
CvScalar s;
//finding the boundaries
for (x=0; x<width-1; x++){
for(y=0; y<height-1; y++){
//getting pixel values
s = cvGet2D(binImage, y, x);
//printf("%f\n", s.val[0]);
//identifying boundaries
if (s.val[0] == 0){
//printf("HELLO");
minX = min(minX, x);
minY = min(minY, y);
maxX = max(maxX, x);
maxY = max(maxY, y);
//printf("%d\n", minY);
}
}
}
//creating rectangle
CvRect rect = cvRect(minX, minY, maxX-minX, maxY-minY);
//sets the part of the image to be cropped
cvSetImageROI(binImage, rect);
IplImage *cropImage = cvCreateImage(cvGetSize(binImage), 8, 1);
IplImage *newImage = cvCreateImage(cvSize(400, 400), 8, 1);
//copies cropped image to new declared image
cvCopy(binImage, cropImage, NULL);
//cvSaveImage("bin/binImage16.jpg", binImage);
cvSaveImage("data set baybayin/cropImage0.jpg", cropImage);
cvResetImageROI(binImage);
cvReleaseImage(&origImage);
cvReleaseImage(&binImage);
cvReleaseImage(&grayImage);
cvReleaseImage(&cropImage);
cvReleaseImage(&newImage);
}
嗨,我可以問我如何加載我的代碼上面的多個圖像?由於此代碼僅加載和處理一個圖像,並且我幾乎無法瞭解一些在線的說明。對不起,我是一個新手。我也想在處理後詢問代碼;如何將新圖像和已處理圖像保存到具有相應文件名的另一個文件夾中?謝謝。加載和處理多個圖像
我不明白你的指示,對不起。如何在特定文件中讀取字符串? – cmsl
他們呢? – misha
我已閱讀關於將所有文件名保存在文本文檔中並閱讀所有文件名以便程序可以找到所需的圖像。就像包含這些內容的文本文檔一樣:00000.jpg 00001.jpg 00002.jpg等等。閱讀這篇文本文檔後,圖像將被加載。但是,我不知道如何實現它。 – cmsl