Exemple #1
0
void CImageSelect::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    CDC *pdc=GetDC();
    if (m_tracker.HitTest(point) < 0)
    {
        CRectTracker track;
        CRect rImage((500-(int)(image.GetWidth()*ZoomFactor))/2,(300-(int)(image.GetHeight()*ZoomFactor))/2,
                     (500-(int)(image.GetWidth()*ZoomFactor))/2+(int)(image.GetWidth()*ZoomFactor),
                     (300-(int)(image.GetHeight()*ZoomFactor))/2+(int)(image.GetHeight()*ZoomFactor));
        if (track.TrackRubberBand(this, point, true))
        {
            track.m_rect.NormalizeRect();
            m_tracker.m_rect = track.m_rect;
            //确保发送图片为正方形
            int nlength = max(track.m_rect.Height(),track.m_rect.Width());
            m_tracker.m_rect.bottom = m_tracker.m_rect.top + nlength;
            m_tracker.m_rect.right = m_tracker.m_rect.left + nlength;
            //确保选取框在图片上
            if (m_tracker.m_rect.left<rImage.left)
            {
                m_tracker.m_rect.right += rImage.left-m_tracker.m_rect.left;
                m_tracker.m_rect.left = rImage.left;
            }
            if (m_tracker.m_rect.top<rImage.top)
            {
                m_tracker.m_rect.bottom += rImage.top-m_tracker.m_rect.top;
                m_tracker.m_rect.top = rImage.top;
            }
            if (m_tracker.m_rect.right>rImage.right)
            {
                m_tracker.m_rect.right = rImage.right;
            }
            if (m_tracker.m_rect.bottom>rImage.bottom)
            {
                m_tracker.m_rect.bottom = rImage.bottom;
            }
            nlength = min(m_tracker.m_rect.Height(),m_tracker.m_rect.Width());
            m_tracker.m_rect.bottom = m_tracker.m_rect.top + nlength;
            m_tracker.m_rect.right = m_tracker.m_rect.left + nlength;
            SetImageRectSelection(&(m_tracker.m_rect));
            m_CheckOk.EnableWindow(TRUE);
        }
        else
        {
            m_tracker.m_rect = CRect(0,0,0,0);
        }
    }
    CRect rect;
    GetClientRect(&rect);
    InvalidateRect(&rect,TRUE);
    showed = false;
    CDialog::OnLButtonDown(nFlags, point);
}
Exemple #2
0
//Create a array of subrectangles (Mat) from each rectangle.
std::vector<std::vector<cv::Mat> > cutRect(const Image_data* src, const std::vector<Rect>& sq )
{    
    //Array to hold the solution
    std::vector<std::vector<cv::Mat> > subrect;
    subrect.clear();

    //Matrix's to extract the subrectangles
    cv::Mat rImage (src->rImage,false) , gImage (src->gImage,false) , bImage (src->bImage,false),img;
    if (DEBUG) img = cv::Mat(src->src,false);

    std::string file;

    //erode images
    //cv::erode(gImage, gImage, cv::Mat(), cv::Point(-1,-1),1); //standard call
    //cv::erode(bImage, bImage, cv::Mat(), cv::Point(-1,-1),1); //standard call

    //Cut and deskew squares
    for ( int i=0 ; i < sq.size() ; i++ ) 
    {
        //Submatrix to fill
        cv::Mat subimg_red, subimg_green , subimg_blue , subimg;

        //auxiliar vector to store the array of Mat
        std::vector<cv::Mat> aux;
        aux.clear();

        // get angle and size from the bounding box
        double angle = sq[i].bounding_box.angle;
        cv::Size box_size = sq[i].bounding_box.size;
        //adjust the angle from "http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/"
        if (angle < -45.) {
            angle += 90.;
            std::swap(box_size.width, box_size.height); // for cropping
        }

        //rotation
        if ( std::abs(angle) > LIMIT_ROTATION ) {
            
            // matrices we'll use
            cv::Mat rotatedR , rotatedG , rotatedB , rot_mat , rotated;
            
            //Rotation Matrix. Same rotation for all
            rot_mat = cv::getRotationMatrix2D(sq[i].bounding_box.center, angle, 1);
            
            //Apply transformation
            cv::warpAffine(rImage, rotatedR, rot_mat, rImage.size(), cv::INTER_LINEAR); // apply the geometric transformation
            cv::warpAffine(gImage, rotatedG, rot_mat, gImage.size(), cv::INTER_LINEAR);
            cv::warpAffine(bImage, rotatedB, rot_mat, bImage.size(), cv::INTER_LINEAR);//INTER_CUBIC
            if (DEBUG) cv::warpAffine(img   , rotated, rot_mat, img.size(), cv::INTER_LINEAR);
            
            //log
            if (DEBUG) {
                file = ANDROID_PATH + "cut_subimg_rot.jpeg";
                cv::imwrite(file,rotated);
            }

            //Cropped image
            cv::getRectSubPix(rotatedR, box_size, sq[i].bounding_box.center, subimg_red);
            cv::getRectSubPix(rotatedG, box_size, sq[i].bounding_box.center, subimg_green);
            cv::getRectSubPix(rotatedB, box_size, sq[i].bounding_box.center, subimg_blue);
            if (DEBUG) cv::getRectSubPix(rotated, box_size , sq[i].bounding_box.center, subimg);
            
            //log
            if (DEBUG) {
                file = ANDROID_PATH + "cut_subimg_crop.jpeg";
                cv::imwrite(file,subimg);
            }
            
        }
        else {//just cut it
            subimg_red   = rImage(sq[i].wrapper_box);
            subimg_green = gImage(sq[i].wrapper_box); 
            subimg_blue  = bImage(sq[i].wrapper_box);
            if (DEBUG) subimg       = img(sq[i].wrapper_box);
        }     
        
        //remove border of tag
        removeBorder(subimg_red);
        removeBorder(subimg_green);
        removeBorder(subimg_blue);
        if (DEBUG) removeBorder(subimg);

        //log
        if (DEBUG) {
            file = ANDROID_PATH + "cut_subimg_borderRemoved.jpeg";
            cv::imwrite(file,subimg);
        }

        //add subimages
        aux.push_back(subimg_red);//R
        aux.push_back(subimg_green);//G
        aux.push_back(subimg_blue);//B
        if (DEBUG) aux.push_back(subimg);//SRC

        subrect.push_back(aux);       
    }

    return subrect;
}
int main(int argc, char *argv[])
{
    char filename[32];
    short int colunas;
    short int linhas;
    int nivelCinza = 4096;
    int dist = 1;
    int op = 0;
    //int *matrizCo_cpu;
    //int *matrizCo_gpu;
    double *matrizCoN_cpu;
    double *matrizCoN_gpu;

    double startTime;
    double stopTime;

    //QCoreApplication a(argc, argv);

    tImage st_image;

    if (argc == 1) {
        colunas = 3000;
        linhas = 3000;

        GerarMatriz g_imagem;
        st_image.vi_colunas = colunas;
        st_image.vi_linhas = linhas;
        //st_image.vi_vector = g_imagem.gerarMatriz(width, nivelCinza);
        st_image.vi_vector = g_imagem.readFileMatriz(colunas);

    } else if (argc == 7) {
        strcpy(filename, argv[1]);
        linhas      = atoi(argv[2]);
        colunas     = atoi(argv[3]);
        nivelCinza  = (int) pow(2, atoi(argv[4]));
        dist        = atoi(argv[5]);
        op          = atoi(argv[6]);

        // Read data of the image raw
        ReadImage rImage(filename, colunas, linhas);
        st_image = rImage.vectorImage_();

    } else {
        cout << "<file> <width> <lenght> <nv> <dist>" << endl;
        //a.exit(EXIT_FAILURE);
        std::exit(EXIT_FAILURE);
    }

    //using namespace imp;

//    std::cout << "Dim: " << st_image.vi_colunas << "x" << st_image.vi_linhas << endl ;
//    std::cout << "GL:   " << nivelCinza << endl;
//    std::cout << endl;

    // **************************************** CPU ******************************************* //

    /*
    MatrizCoocorrencia Mc(st_image.vi_vector, colunas);

    startTime = omp_get_wtime();

    //matrizCo_cpu = Mc.calcularMatrizCoocorrencia(nivelCinza, angulo, distancia);
    //matrizCoN_cpu = Mc.normalizar(matrizCo_cpu, nivelCinza);
    matrizCoN_cpu = Mc.calcularMatrizMedia(nivelCinza, dist); // double

    stopTime = omp_get_wtime();

    cout << "Cpu Time: " << stopTime - startTime << endl;
    //*/

    // **************************************** CUDA ****************************************** //

    mcGPU MatrizC;
    startTime = omp_get_wtime();
    matrizCoN_gpu = MatrizC.matrizCoN(st_image, nivelCinza, dist);
    stopTime = omp_get_wtime();
//    cout << "GPU Time: " << stopTime - startTime << endl;

    // Plot Matriz
    //Img::ConstruirImgMC(matrizCoN_cpu, nivelCinza);
    //cout << "**Gerada IMG da Matriz: " << endl;

    // ************************************************************************************** //
    /*
    if (!cmp::compara(matrizCoN_cpu, matrizCoN_gpu, nivelCinza)) {
        exit(EXIT_FAILURE);
    }
    //*/
    // ************************************************************************************** //

    atGpu Atrib(matrizCoN_gpu, nivelCinza);
    atCpu atCpu(matrizCoN_gpu, nivelCinza);

    double valor = 0.0;

    cout.unsetf ( std::ios::floatfield );
    cout.precision(15);

//    cout << std::fixed << endl;
/*
    tempo::start();
    valor = atCpu.max();
    tempo::stop();
    cout << "          MAX: " << valor << endl << endl;
    valor = Atrib.AtMax();
    cout << "      gpu MAX: " << valor << endl << endl;

    tempo::start();
    valor = atCpu.min();
    tempo::stop();
    cout << "          MIN: " << valor << endl << endl;
    valor = Atrib.AtMin();
    cout << "      gpu MIN: " << valor << endl << endl;

    tempo::start();
    valor = atCpu.media();
    tempo::stop();
    cout << "        Média: " << valor << endl << endl;
    valor = Atrib.AtMedia();
    cout << "    gpu Média: " << valor << endl << endl;

    tempo::start();
    valor = atCpu.mediana();
    tempo::stop();
    cout << "      Mediana: " << valor << endl << endl;
    valor = Atrib.AtMediana();
    cout << "  gpu Mediana: " << valor << endl << endl;
*/
    /*
//    tempo::start();
//    valor = atCpu.variancia();
//    tempo::stop();
//    cout << "    Variância: " << valor << endl << endl;
    valor = Atrib.AthVariancia();
    cout << "gpu Variância: " << valor << endl << endl;

    tempo::start();
    valor = atCpu.correlacao();
    tempo::stop();
    cout << "  *Correlacao: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.contraste();
//    tempo::stop();
//    cout << "    Contraste: " << valor << endl << endl;
    valor = Atrib.AthContraste();
    cout << "gpu Contraste: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.entropia();
//    tempo::stop();
//    cout << "     Entropia: " << valor << endl << endl;
    valor = Atrib.AthEntropia();
    cout << " gpu Entropia: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.somaEntropia();
//    tempo::stop();
//    cout << "Soma Entropia: " << valor << endl << endl;
    valor = Atrib.AthSomaEntropia();
    cout << "gpu Soma Ent.: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.diferencaEntropia();
//    tempo::stop();
//    cout << " Dif Entropia: " << valor << endl << endl;
    valor = Atrib.AthDifEntropia();
    cout << "gpu Dif. Ent.: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.mdi();
//    tempo::stop();
//    cout << "          MDI: " << valor << endl << endl;
    valor = Atrib.AthMDI();
    cout << "      gpu MDI: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.mediaSoma();
//    tempo::stop();
//    cout << "   Media Soma: " << valor << endl << endl;
    valor = Atrib.AthMediaSoma();
    cout << "gpu Med. Soma: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.varianciaSoma();
//    tempo::stop();
//    cout << "VarianciaSoma: " << valor << endl << endl;
    valor = Atrib.AthVarSoma();
    cout << "gpu Var. Soma: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.mediaDiferenca();
//    tempo::stop();
//    cout << "    Media Dif: " << valor << endl << endl;
    valor = Atrib.AthMediaDif();
    cout << "gpu Media Dif: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.varianciaDiferenca();
//    tempo::stop();
//    cout << "Variancia Dif: " << valor << endl << endl;
    valor = Atrib.AthVarDif();
    cout << " gpu Var. Dif: " << valor << endl << endl;

//    tempo::start();
//    valor = atCpu.energia();
//    tempo::stop();
//    cout << "      Energia: " << valor << endl << endl;
    valor = Atrib.AthEnergia();
    cout << "  gpu Energia: " << valor << endl << endl;
    // */
    if (op == 1 || op == 0) {
//        cout << "Engergia" << endl;
        valor = Atrib.AthEnergia();
//        cout << "      Energia: " << valor << endl << endl;
    } if (op == 2 || op == 0) {
//        cout << "Contraste" << endl;
        valor = Atrib.AthContraste();
//        cout << "    Contraste: " << valor << endl << endl;
    } if (op == 3 || op == 0) {
//        cout << "Correlacao" << endl;
        //valor = Atrib.AthCorrelacao();
//        cout << "   Correlacao: " << valor << endl << endl;
    } if (op == 4 || op == 0) {
//        cout << "Variancia" << endl;
        valor = Atrib.AthVariancia();
//        cout << "    Variância: " << valor << endl << endl;
    } if (op == 5 || op == 0) {
//        cout << "MDI" << endl;
        valor = Atrib.AthMDI();
//        cout << "          MDI: " << valor << endl << endl;
    } if (op == 6 || op == 0) {
//        cout << "Entropia" << endl;
        valor = Atrib.AthEntropia();
//        cout << "     Entropia: " << valor << endl << endl;
    } if (op == 7 || op == 0) {
//        cout << "Media Soma" << endl;
        valor = Atrib.AthMediaSoma();
//        cout << "   Media Soma: " << valor << endl << endl;
    } if (op == 8 || op == 0) {
//        cout << "Var Soma" << endl;
        valor = Atrib.AthVarSoma();
//        cout << "VarianciaSoma: " << valor << endl << endl;
    } if (op == 9 || op == 0) {
//        cout << "Soma Entr." << endl;
        valor = Atrib.AthSomaEntropia();
//        cout << "Soma Entropia: " << valor << endl << endl;
    } if (op == 10 || op == 0) {
//        cout << "Var Dif." << endl;
        valor = Atrib.AthVarDif();
//        cout << "Variancia Dif: " << valor << endl << endl;
    } if (op == 11 || op == 0) {
//        cout << "Dif Entr." << endl;
        valor =Atrib.AthDifEntropia();
//        cout << " Dif Entropia: " << valor << endl << endl;
    }
    // ************************************************************************************** //

    //arq::arquivo("_cpu.txt", matrizCoN_cpu, nivelCinza);
    //arq::arquivo("_gpu.txt", matrizCoN_cpu, nivelCinza);

    // ************************************************************************************** //

    //delete [] matrizCo_cpu;
    //delete [] matrizCo_gpu;
    //delete [] matrizCoN_cpu;
    if (matrizCoN_gpu) delete [] matrizCoN_gpu;

    // ************************************************************************************** //

    //return a.exec();
    //a.exit(EXIT_SUCCESS);
    return 0;
}
void CalPainter::paint(int month)
{
    if (!device())
    {
        return;
    }

    int width  = device()->width();
    int height = device()->height();

    CalSettings* settings = CalSettings::instance();
    CalParams& params     = CalSettings::instance()->params;

    // --------------------------------------------------

    // FIXME: magic number 42
    int days[42];
    int startDayOffset = KGlobal::locale()->weekStartDay();

    for (int i = 0; i < 42; ++i)
    {
        days[i] = -1;
    }

    QDate d;
    KGlobal::locale()->calendar()->setDate(d, params.year, month, 1);
    int s = d.dayOfWeek();

    if (s + 7 - startDayOffset >= 7)
    {
        s = s - 7;
    }

    for (int i = s; i < (s + KGlobal::locale()->calendar()->daysInMonth(d)); ++i)
    {
        days[i + (7 - startDayOffset)] = i - s + 1;
    }

    // -----------------------------------------------

    QRect rCal(0, 0, 0, 0);
    QRect rImage(0, 0, 0, 0);
    QRect rCalHeader(0, 0, 0, 0);

    int cellSizeX;
    int cellSizeY;

    switch (params.imgPos)
    {
        case (CalParams::Top):
        {
            rImage.setWidth(width);
            rImage.setHeight((int)(height * params.ratio / (params.ratio + 100)));

            int remainingHeight = height - rImage.height();
            cellSizeX           = (width - 20) / 7;
            cellSizeY           = remainingHeight / 8;

            rCal.setWidth(cellSizeX * 7);
            rCal.setHeight(cellSizeY * 7);

            rCalHeader.setWidth(rCal.width());
            rCalHeader.setHeight(cellSizeY);
            rCalHeader.moveTop(rImage.bottom());
            rCalHeader.moveLeft(width / 2 - rCalHeader.width() / 2);

            rCal.moveTopLeft(rCalHeader.bottomLeft());

            break;
        }

        case (CalParams::Left):
        {
            rImage.setHeight(height);
            rImage.setWidth((int)(width * params.ratio / (params.ratio + 100)));

            int remainingWidth  = width - rImage.width();
            cellSizeX           = (remainingWidth - 20) / 8;
            cellSizeY           = height / 8;

            rCal.setWidth(cellSizeX * 7);
            rCal.setHeight(cellSizeY * 7);

            rCalHeader.setWidth(rCal.width());
            rCalHeader.setHeight(cellSizeY);
            rCalHeader.moveLeft(rImage.right() + cellSizeX);

            rCal.moveTopLeft(rCalHeader.bottomLeft());

            break;
        }

        case (CalParams::Right):
        {
            rImage.setHeight(height);
            rImage.setWidth((int)(width * params.ratio / (params.ratio + 100)));

            int remainingWidth  = width - rImage.width();
            cellSizeX           = (remainingWidth - 20) / 8;
            cellSizeY           = height / 8;

            rCal.setWidth(cellSizeX * 7);
            rCal.setHeight(cellSizeY * 7);

            rCalHeader.setWidth(rCal.width());
            rCalHeader.setHeight(cellSizeY);
            rCal.moveTop(rCalHeader.bottom());

            rImage.moveLeft(width - rImage.width());

            break;
        }

        default:
            return;
    }

    int fontPixels = cellSizeX / 3;
    params.baseFont.setPixelSize(fontPixels);

    // ---------------------------------------------------------------

    fillRect(0, 0, width, height, Qt::white);
    setFont(params.baseFont);

    // ---------------------------------------------------------------

    save();
    QFont f(params.baseFont);
    f.setBold(true);
    f.setPixelSize(f.pixelSize() + 5);
    setFont(f);
    drawText(rCalHeader, Qt::AlignLeft | Qt::AlignVCenter, QString::number(params.year));
    drawText(rCalHeader, Qt::AlignRight | Qt::AlignVCenter,
             KGlobal::locale()->calendar()->monthName(month, params.year));
    restore();

    // ---------------------------------------------------------------

    int   sx, sy;
    QRect r, rsmall, rSpecial;

    r.setWidth(cellSizeX);
    r.setHeight(cellSizeY);

    int index = 0;

    save();

    setPen(Qt::red);
    sy = rCal.top();

    for (int i = 0; i < 7; ++i)
    {
        int dayname = i + startDayOffset;

        if (dayname > 7)
        {
            dayname = dayname - 7;
        }

        sx = cellSizeX * i + rCal.left();
        r.moveTopLeft(QPoint(sx, sy));
        rsmall = r;
        rsmall.setWidth(r.width() - 2);
        rsmall.setHeight(r.height() - 2);
        drawText(rsmall, Qt::AlignRight | Qt::AlignBottom,
                 KGlobal::locale()->calendar()->weekDayName(dayname, KCalendarSystem::ShortDayName));
    }

    restore();

    for (int j = 0; j < 6; ++j)
    {
        sy = cellSizeY * (j + 1) + rCal.top();

        for (int i = 0; i < 7; ++i)
        {
            sx     = cellSizeX * i + rCal.left();
            r.moveTopLeft(QPoint(sx, sy));
            rsmall = r;
            rsmall.setWidth(r.width() - 2);
            rsmall.setHeight(r.height() - 2);

            if (days[index] != -1)
            {
                if (settings->isSpecial(month, days[index]))
                {
                    save();
                    setPen(settings->getDayColor(month, days[index]));
                    drawText(rsmall, Qt::AlignRight | Qt::AlignBottom,
                             QString::number(days[index]));

                    QString descr = settings->getDayDescr(month, days[index]);
                    kDebug() << "Painting special info: '" << descr
                             << "' for date " << days[index] << "/"
                             << month;
                    rSpecial = rsmall;
                    rSpecial.translate(2, 0);
                    QFont f(params.baseFont);
                    f.setPixelSize(f.pixelSize() / 3);
                    setFont(f);

                    drawText(rSpecial, Qt::AlignLeft | Qt::AlignTop, descr);

                    restore();
                }
                else
                {
                    drawText(rsmall, Qt::AlignRight | Qt::AlignBottom,
                             QString::number(days[index]));
                }
            }

            index++;
        }
    }

    // ---------------------------------------------------------------

    if (params.drawLines)
    {
        sx = rCal.left();

        for (int j = 0; j < 8; ++j)
        {
            sy = cellSizeY * j + rCal.top();
            drawLine(sx, sy, rCal.right(), sy);
        }

        sy = rCal.top();

        for (int i = 0; i < 8; ++i)
        {
            sx = cellSizeX * i + rCal.left();
            drawLine(sx, sy, sx, rCal.bottom());
        }
    }

    // Check if RAW file.
    QFileInfo fi(imagePath_.path());

    QString rawFilesExt(KDcrawIface::KDcraw::rawFiles());

    if (rawFilesExt.toUpper().contains(fi.suffix().toUpper()))
    {
        KDcrawIface::KDcraw::loadDcrawPreview(image_, imagePath_.path());
    }
    else
    {
        image_.load(imagePath_.path());
    }

    if (image_.isNull())
    {
        fillRect(rImage, Qt::blue);
    }
    else
    {
        if ( orientation_ != KPMetadata::ORIENTATION_UNSPECIFIED )
        {
            QMatrix matrix = RotationMatrix::toMatrix(orientation_);
            image_         = image_.transformed( matrix );
        }

        emit signalProgress(0);
        image_ = image_.scaled(rImage.width(), rImage.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);

        emit signalTotal(image_.height());

        int h = image_.height();
        int x = rImage.bottomLeft().x() + (rImage.width() - image_.width()) / 2;
        int y = (rImage.height() - h) / 2;

        int blockSize = 10;
        int block = 0;
        while (block < h && !cancelled_)
        {
            if (block + blockSize > h)
            {
                blockSize = h - block;
            }
            drawImage(x, y + block, image_, 0, block, image_.width(), blockSize);
            block += blockSize;
            emit signalProgress(block);
        }

        emit signalFinished();
    }
}