Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to translate this code to Java

 if (edges.at<unsigned char="">(j,i) == 255)
            {
                //Calculating maxP and minP within Win-region:
                 char minP = rgba.at(j,i);
                 char maxP = rgba.at<unsigned char="">(j,i);
                int offsetInWin = Win/2;

                for (int m = - offsetInWin; m < offsetInWin; m++) {
                    for (int n = - offsetInWin; n < offsetInWin; n++) {
                        if (rgba.at<unsigned char="">(j+m,i+n) < minP) {
                            minP = rgba.at<unsigned char="">(j+m,i+n);
                        }else if (rgba.at<unsigned char="">(j+m,i+n) > maxP) {
                            maxP = rgba.at<unsigned char="">(j+m,i+n);
                        }
                    }
                }

How to translate this code to Java

This code is from Azoft to read embossed text from credit cards (https://www.azoft.com/blog/ocr-embossed-text). I'm trying to integrate it with my school project, they gave an example code using C, and Im trying to translate it to JAVA.

(void)processingByStrokesMethod:(cv::Mat)src dst:(cv::Mat*)dst { 
cv::Mat tmp;  
cv::GaussianBlur(src, tmp, cv::Size(3,3), 2.0);                    // gaussian blur  
tmp = cv::abs(src - tmp);                                          // matrix of differences between source image and blur iamge  

//Binarization:  
cv::threshold(tmp, tmp, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);  

//Using method of strokes:  
int Wout = 12;  
int Win = Wout/2;  
int startXY = Win;  
int endY = src.rows - Win;  
int endX = src.cols - Win;  

for (int j = startXY; j < endY; j++) {  
    for (int i = startXY; i < endX; i++) {  
        //Only edge pixels:  
        if (edges.at<unsigned (tmp.at<unsigned char="">(j,i) == 255)
            {
    255)  
        {  
            //Calculating maxP and minP within Win-region:
                 Win-region:  
            unsigned char minP = rgba.at(j,i);
                 src.at<unsigned char="">(j,i);  
            unsigned char maxP = rgba.at<unsigned char="">(j,i);
    src.at<unsigned char="">(j,i);  
            int offsetInWin = Win/2;

    Win/2;  

            for (int m = - offsetInWin; m < offsetInWin; m++) {
    {  
                for (int n = - offsetInWin; n < offsetInWin; n++) {
    {  
                    if (rgba.at<unsigned (src.at<unsigned char="">(j+m,i+n) < minP) {
    {  
                        minP = rgba.at<unsigned char="">(j+m,i+n);
    src.at<unsigned char="">(j+m,i+n);  
                    }else if (rgba.at<unsigned (src.at<unsigned char="">(j+m,i+n) > maxP) {
    {  
                        maxP = rgba.at<unsigned char="">(j+m,i+n);
                        }
                    }
                src.at<unsigned char="">(j+m,i+n);  
                    }  
                }  
            }  

            //Voiting:  
            unsigned char meanP = lroundf((minP+maxP)/2.0);  

            for (int l = -Win; l < Win; l++) {  
                for (int k = -Win; k < Win; k++) {  
                    if (src.at<unsigned char="">(j+l,i+k) >= meanP) {  
                        dst->at<unsigned char="">(j+l,i+k)++;  
                    }  
                }  
            }  
        }  
    }  
}  

///// Normalization of imageOut:  
unsigned char maxValue = dst->at<unsigned char="">(0,0);  

for (int j = 0; j < dst->rows; j++) {              //finding max value of imageOut  
    for (int i = 0; i < dst->cols; i++) {  
        if (dst->at<unsigned char="">(j,i) > maxValue)  
            maxValue = dst->at<unsigned char="">(j,i);  
    }  
}  
float knorm = 255.0 / maxValue;  

for (int j = 0; j < dst->rows; j++) {             //normalization of imageOut  
    for (int i = 0; i < dst->cols; i++) {  
        dst->at<unsigned char="">(j,i) = lroundf(dst->at<unsigned char="">(j,i)*knorm);  
    }  
}