首页 > 代码库 > 保存·

保存·

#include<iostream>#include<string>#include<list>#include<vector>#include<map>#include<stack>#include <opencv2/core/core.hpp>#include<opencv2/imgproc/imgproc.hpp>#include<opencv2/highgui/highgui.hpp>typedef unsigned char byte;void GetImgD_SIO(IplImage *src,byte *imagedata){    if (src->nChannels!=1)    {        return;    }    int width=src->width;    int height=src->height;    for(int i=0;i<height;i++)    {        for (int j=0;j<width;j++)        {            imagedata[i*width+j]=src->imageData[i*src->widthStep+j];        }    }}void SetImgD_SIO(IplImage *src,byte *imagedata){    int width=src->width;    int height=src->height;    for(int i=0;i<height;i++)    {        for (int j=0;j<width;j++)        {            src->imageData[i*src->widthStep+j]=imagedata[i*width+j];        }    }}int GetTwoValueT(byte *image,int width,int height){    int Hdata[256];    memset(Hdata,0,256*sizeof(int));    int minPixel=255;    int maxPixel=0;    int T=0;    int TT=0;    for (int i=0;i<height;i++)    {        for (int j=0;j<width;j++)        {            Hdata[image[i*width+j]]++;            if (minPixel>image[i*width+j])            {                minPixel=image[i*width+j];            }            if (maxPixel<image[i*width+j])            {                maxPixel=image[i*width+j];            }        }    }    long IP1,IP2,SP1,SP2;    int MeanG1=0;    int MeanG2=0;    double w0,w1;    double G,tmpG;    G=0;    tmpG=0;    for (int i=minPixel;i<=maxPixel;i++)    {        T=i;        IP1=0;        IP2=0;        SP1=0;        SP2=0;        for (int j=minPixel;j<=T;j++)        {            IP1+=Hdata[j]*j;            SP1+=Hdata[j];        }        if (SP1!=0)        {            MeanG1=IP1/SP1;        }        else        {            MeanG1=0;        }        w0=(double)SP1/(double)(width*height);        for (int j=T+1;j<=maxPixel;j++)        {            IP2+=Hdata[j]*j;            SP2+=Hdata[j];        }        if (SP2!=0)        {            MeanG2=IP2/SP2;        }        else        {            MeanG2=0;        }        w1=1-w0;        G=(double)((w0*w1)*(MeanG1-MeanG2)*(MeanG1-MeanG2));        if (G>tmpG)        {            tmpG=G;            TT=T;        }    }    return TT;}void TwoValue(byte *imagedata,int width,int height,int T){    for(int i=0;i<height;i++)    {        for (int j=0;j<width;j++)        {            int tmp=imagedata[i*width+j];            imagedata[i*width+j]=tmp>T?0:255;        }    }}void icvprCcaByTwoPass(const cv::Mat&  _binImg,cv::Mat& _lableImg){    if(_binImg.empty()||_binImg.type()!=CV_8UC1)    {        return ;    }    //first pass    _lableImg.release();    _binImg.convertTo(_lableImg,CV_32SC1);    int label=1;    std::vector<int>labelSet;    labelSet.push_back(0);    labelSet.push_back(1);    int rows=_binImg.rows-1;    int cols=_binImg.cols-1;    for(int i=1;i<rows;i++)    {        int *data_preRow=_lableImg.ptr<int>(i-1);        int *data_curRow=_lableImg.ptr<int>(i);        for(int j=1;j<cols;j++)        {            if(data_curRow[j]==1)            {                std::vector<int>  neighborLabels;                neighborLabels.reserve(2);                int leftPixel=data_curRow[j-1];                int upPixel=data_preRow[j];                if(leftPixel>1)                {                    neighborLabels.push_back(leftPixel);                }                if (upPixel > 1)                {                    neighborLabels.push_back(upPixel) ;                }                if (neighborLabels.empty())                {                    labelSet.push_back(++label) ;                     data_curRow[j] = label ;                    labelSet[label] = label ;                }                else                {                    std::sort(neighborLabels.begin(), neighborLabels.end()) ;                    int smallestLabel = neighborLabels[0] ;                      data_curRow[j] = smallestLabel ;                     // save equivalence                    for (size_t k = 1; k < neighborLabels.size(); k++)                    {                        int tempLabel = neighborLabels[k] ;                        int& oldSmallestLabel = labelSet[tempLabel] ;                        if (oldSmallestLabel > smallestLabel)                        {                                                       labelSet[oldSmallestLabel] = smallestLabel ;                            oldSmallestLabel = smallestLabel ;                        }                                               else if (oldSmallestLabel < smallestLabel)                        {                            labelSet[smallestLabel] = oldSmallestLabel ;                        }                    }                }                  }        }    }    for (size_t i = 2; i < labelSet.size(); i++)    {        int curLabel = labelSet[i] ;        int preLabel = labelSet[curLabel] ;        while (preLabel != curLabel)        {            curLabel = preLabel ;            preLabel = labelSet[preLabel] ;        }        labelSet[i] = curLabel ;    }    for (int i = 0; i < rows; i++)    {        int* data = http://www.mamicode.com/_lableImg.ptr<int>(i) ;        for (int j = 0; j < cols; j++)        {            int& pixelLabel = data[j] ;            pixelLabel = labelSet[pixelLabel] ;         }    }}cv::Scalar icvprGetRandomColor(){    uchar r = 255 * (rand()/(1.0 + RAND_MAX));    uchar g = 255 * (rand()/(1.0 + RAND_MAX));    uchar b = 255 * (rand()/(1.0 + RAND_MAX));    return cv::Scalar(b,g,r) ;}void icvprLabelColor(const cv::Mat& _labelImg, cv::Mat& _colorLabelImg) {    if (_labelImg.empty() ||        _labelImg.type() != CV_32SC1)    {        return ;    }     std::map<int, cv::Scalar> colors ;     int rows = _labelImg.rows ;    int cols = _labelImg.cols ;     _colorLabelImg.release() ;    _colorLabelImg.create(rows, cols, CV_8UC3) ;    _colorLabelImg = cv::Scalar::all(0) ;     for (int i = 0; i < rows; i++)    {        const int* data_src = http://www.mamicode.com/(int*)_labelImg.ptr<int>(i) ;        uchar* data_dst = _colorLabelImg.ptr<uchar>(i) ;        for (int j = 0; j < cols; j++)        {            int pixelValue =http://www.mamicode.com/ data_src[j] ;            if (pixelValue > 1)            {                if (colors.count(pixelValue) <= 0)                {                    colors[pixelValue] = icvprGetRandomColor() ;                }                cv::Scalar color = colors[pixelValue] ;                *data_dst++ = color[0] ;                *data_dst++ = color[1] ;                *data_dst++ = color[2] ;            }            else            {                data_dst++ ;                data_dst++ ;                data_dst++ ;            }        }    }}int main(int argc,char **argv){    IplImage* img1 = cvLoadImage("E:/newocr/ocr/big.jpg", 1);    int width=img1->width;    int height=img1->height;    std::cout<<width<<"  "<<height<<std::endl;    byte *imagedata=http://www.mamicode.com/new byte[width*height];    GetImgD_SIO(img1,imagedata);    int T=GetTwoValueT(imagedata,width,height);    std::cout<<T<<std::endl;    TwoValue(imagedata,width,height,T);    for (int i=0;i<height;i++)    {        for (int j=0;j<width;j++)        {            if (imagedata[i*width+j]==255)            {                imagedata[i*width+j]=1;            }        }    }    SetImgD_SIO(img1,imagedata);    cv::Mat binImage(img1);    cv::imshow("test1",binImage);    //cv::Mat binImage = cv::imread("E:/newocr/ocr/big.jpg", 0) ;    //cv::imshow("test",binImage);    //cv::threshold(binImage,binImage,50,1,CV_THRESH_BINARY_INV);        /*    cv::Mat  labelImg;    icvprCcaByTwoPass(binImage,labelImg);    //cv::imshow("test1",binImage);    //cv::imshow("test2",labelImg);    cv::Mat grayImg ;    labelImg *= 10 ;    labelImg.convertTo(grayImg, CV_8UC1) ;    cv::imshow("labelImg", grayImg) ;     cv::Mat colorLabelImg ;    icvprLabelColor(labelImg, colorLabelImg) ;    cv::imshow("colorImg", colorLabelImg) ;*/    cv::waitKey(0);    }