Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

Matlab

[m,n]=size(imTmpImg);
    hor_thresh=22; 
    one_count=0; 
    zero_flag=0;
    hor_image=imTmpImg;
    for i=1:m 
        for j=1:n 
            if(imTmpImg(i,j)==0) 
                if(zero_flag==1) 
                    if(one_count<=hor_thresh) 
                        hor_image(i,j-one_count:j-1)=0;
                    else
                        zero_flag=1;
                    end
                    one_count=0; 
                end
                zero_flag=1; 
            else
                if(zero_flag==1)
                    one_count=one_count+1;
                end
            end
        end
    end

In C++

Size size = tmpImg.size();
int hor_thres = 22;
int one_count = 0;
int zero_flag = 0;
Mat tmpImgConnected = Mat(tmpText.size(), CV_8UC1, Scalar(0, 0, 0));
for (int i = 0; i<size.width; i++){
    for (int j = 0; j<size.height; i++){
        if (tmpText.at<uchar>(i, j) == 0){   //ERROR
            if (zero_flag == 1){
            if (one_count <= hor_thres){
                for (int col = j - one_count - 1; col < j - 1; j++) { 
                            tmpTextConnected.at<uchar>(i - 1, col) = 0;
                }
            }
            else
            {
                zero_flag = 1;
            }
            one_count = 0;
            }
            zero_flag = 1;
        }
        else
        {
            if (zero_flag == 1)
            {
               one_count = one_count + 1;
            }
        }
    }
}

I got this error :

Unhandled exception at 0x000000013F579F98 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000009D3802C.

It error in this line

if (tmpText.at<uchar>(i, j) == 0){   //ERROR

Anyidea?

Thank

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

Matlab

[m,n]=size(imTmpImg);
    hor_thresh=22; 
    one_count=0; 
    zero_flag=0;
    hor_image=imTmpImg;
    for i=1:m 
        for j=1:n 
            if(imTmpImg(i,j)==0) 
                if(zero_flag==1) 
                    if(one_count<=hor_thresh) 
                        hor_image(i,j-one_count:j-1)=0;
                    else
                        zero_flag=1;
                    end
                    one_count=0; 
                end
                zero_flag=1; 
            else
                if(zero_flag==1)
                    one_count=one_count+1;
                end
            end
        end
    end

In C++

Size size = tmpImg.size();
int hor_thres = 22;
int one_count = 0;
int zero_flag = 0;
Mat tmpImgConnected = Mat(tmpText.size(), CV_8UC1, Scalar(0, 0, 0));
for (int i = 0; i<size.width; i++){
    for (int j = 0; j<size.height; i++){
        if (tmpText.at<uchar>(i, j) == 0){   //ERROR
            if (zero_flag == 1){
            if (one_count <= hor_thres){
                for (int col = j - one_count - 1; col < j - 1; j++) { 
                            tmpTextConnected.at<uchar>(i - 1, col) = 0;
                }
            }
            else
            {
                zero_flag = 1;
            }
            one_count = 0;
            }
            zero_flag = 1;
        }
        else
        {
            if (zero_flag == 1)
            {
               one_count = one_count + 1;
            }
        }
    }
}

I got this error :

Unhandled exception at 0x000000013F579F98 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000009D3802C.

It error in this line

if (tmpText.at<uchar>(i, j) == 0){   //ERROR

Another issue is

hor_image(i,j-one_count:j-1)=0;

Is it the right way to implement the same thing in C++?

Anyidea?

Thank

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

Matlab

[m,n]=size(imTmpImg);
    hor_thresh=22; 
    one_count=0; 
    zero_flag=0;
    hor_image=imTmpImg;
    for i=1:m 
        for j=1:n 
            if(imTmpImg(i,j)==0) 
                if(zero_flag==1) 
                    if(one_count<=hor_thresh) 
                        hor_image(i,j-one_count:j-1)=0;
                    else
                        zero_flag=1;
                    end
                    one_count=0; 
                end
                zero_flag=1; 
            else
                if(zero_flag==1)
                    one_count=one_count+1;
                end
            end
        end
    end

In C++

Size size = tmpImg.size();
int hor_thres = 22;
int one_count = 0;
int zero_flag = 0;
Mat tmpImgConnected = Mat(tmpText.size(), CV_8UC1, Scalar(0, 0, 0));
for (int i = 0; i<size.width; i++){
    for (int j = 0; j<size.height; i++){
        if (tmpText.at<uchar>(i, j) == 0){   //ERROR
            if (zero_flag == 1){
            if (one_count <= hor_thres){
                for (int col = j - one_count - 1; col < j - 1; j++) { 
                            tmpTextConnected.at<uchar>(i - 1, col) = 0;
                }
            }
            else
            {
                zero_flag = 1;
            }
            one_count = 0;
            }
            zero_flag = 1;
        }
        else
        {
            if (zero_flag == 1)
            {
               one_count = one_count + 1;
            }
        }
    }
}

I got this error :

Unhandled exception at 0x000000013F579F98 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000009D3802C.
  1. It error in this line

    if (tmpText.at<uchar>(i, (tmpText.at(i, j) == 0){ //ERROR

    //ERROR

  2. Another issue is

    hor_image(i,j-one_count:j-1)=0;
    

    hor_image(i,j-one_count:j-1)=0;

    Is it the right way to implement the the same thing in C++?

  3. Since cv::Mat is vector structure, aren't there any way to implement in vector way?

Anyidea?

Thank

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

MatlabMatLabCode

[m,n]=size(imTmpImg);
    hor_thresh=22; 
    one_count=0; 
    zero_flag=0;
    hor_image=imTmpImg;
     hor_thresh=20;
zeros_count=0;
one_flag=0;
hor_image=image;
for i=1:m 
    i=1:m
    for j=1:n 
            if(imTmpImg(i,j)==0) 
                if(zero_flag==1) 
                    if(one_count<=hor_thresh) 
                        hor_image(i,j-one_count:j-1)=0;
    j=1:n
        if(image(i,j)==1)
            if(one_flag==1)
                if(zeros_count<=hor_thresh)
                    hor_image(i,j-zeros_count:j-1)=1;
                else
                        zero_flag=1;
    one_flag=0;
                end
                    one_count=0; 
    zeros_count=0;
            end
                zero_flag=1; 
            else
                if(zero_flag==1)
                    one_count=one_count+1;
    one_flag=1;
        else 
            if(one_flag==1)
                zeros_count=zeros_count+1;
            end
         end
     end
 end

In C++I tried to implement in C++ Code

Size size = tmpImg.size();
 int hor_thres = 22;
 int one_count = 0;
 int zero_flag = 0;
 Mat tmpImgConnected tmpImg = Mat(tmpText.size(), Mat(Img.size(), CV_8UC1, Scalar(0, 0, 0));
                for (int j = 0; j<Img.rows; j++){
                    for (int i = 0; i<size.width; i++){
    for (int j = 0; j<size.height; i++){
i<Img.cols; j++){
                        if (tmpText.at<uchar>(i, j) (Img.at<uchar>(j, i) == 0){   //ERROR
            if (zero_flag == 1){
            if (one_count <= hor_thres){
                for (int col = j - one_count - 1; col < j - 1; j++) { 
                            tmpTextConnected.at<uchar>(i - 1, col) = 0;
                }
            }
            else
0)
                        {
                zero_flag = 1;
            }
            one_count = 0;
            }
            zero_flag = 1;
        }
        else
        {
            if (zero_flag == 1)
             {
                                if (one_count <= hor_thres)
                                {           
                                    tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
// I want to do the same thing in Matlab as this  image(i,j-one_count:j-1)=0;
                                }
                                else
                                {
                                    zero_flag = 1;
                                }
                                one_count = 0;
                            }
                            zero_flag = 1;
                        }
                        else
                        {
                            if (zero_flag == 1)
                            {
                                one_count = one_count + 1;
             }
         }
     }
 }

I got this This time no error :but the result is not expected ..

The issue is the way i want to write c++ code the same thing as

Matlab

Unhandled exception at 0x000000013F579F98 in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x0000000009D3802C.
tmpImg(i,j-one_count:j-1)=0;
  1. It error

    C++

    tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
    

    Anyidea???

    Another thing is in this line

    if (tmpText.at(i, j) == 0){ //ERROR

  2. Another issue is

    hor_image(i,j-one_count:j-1)=0;

    Is it Matlab the right way to implement the same thing in C++?

  3. Since cv::Mat is vector structure, aren't there any way to implement in vector way?

Anyidea?index start from 1 while C++ start from 0.

Thank

click to hide/show revision 5
retagged

updated 2014-02-07 10:39:29 -0600

berak gravatar image

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

MatLabCode

 hor_thresh=20;
zeros_count=0;
one_flag=0;
hor_image=image;
for i=1:m
    for j=1:n
        if(image(i,j)==1)
            if(one_flag==1)
                if(zeros_count<=hor_thresh)
                    hor_image(i,j-zeros_count:j-1)=1;
                else
                    one_flag=0;
                end
                zeros_count=0;
            end
            one_flag=1;
        else 
            if(one_flag==1)
                zeros_count=zeros_count+1;
            end
        end
    end
end

I tried to implement in C++ Code

    int hor_thres = 22;
                int one_count = 0;
                int zero_flag = 0;
                Mat tmpImg = Mat(Img.size(), CV_8UC1, Scalar(0, 0, 0));
                for (int j = 0; j<Img.rows; j++){
                    for (int i = 0; i<Img.cols; j++){
                        if (Img.at<uchar>(j, i) == 0)
                        {
                            if (zero_flag == 1)
                            {
                                if (one_count <= hor_thres)
                                {           
                                    tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
// I want to do the same thing in Matlab as this  image(i,j-one_count:j-1)=0;
                                }
                                else
                                {
                                    zero_flag = 1;
                                }
                                one_count = 0;
                            }
                            zero_flag = 1;
                        }
                        else
                        {
                            if (zero_flag == 1)
                            {
                                one_count = one_count + 1;
                            }
                        }
                    }
                }

This time no error but the result is not expected ..

The issue is the way i want to write c++ code the same thing as

Matlab

tmpImg(i,j-one_count:j-1)=0;

C++

tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));

Anyidea???

Another thing is in Matlab the index start from 1 while C++ start from 0.

Thank

click to hide/show revision 6
retagged

updated 2014-02-07 10:39:50 -0600

berak gravatar image

Implementation Run Length Smoothing Algorithm in C++

This is my old question related to RLSA in C++, but I havent got any help yet.

I tried to implement the code from Matlab to C++

The description of this algorithm :

http://crblpocr.blogspot.fr/2007/06/run-length-smoothing-algorithm-rlsa.html http://crblpocr.blogspot.fr/2007/06/determination-of-run-length-smoothing.html

There is RLSA implementation in Matlab by this thread :

http://mathworks.cn/matlabcentral/newsreader/view_thread/318198

MatLabCode

 hor_thresh=20;
zeros_count=0;
one_flag=0;
hor_image=image;
for i=1:m
    for j=1:n
        if(image(i,j)==1)
            if(one_flag==1)
                if(zeros_count<=hor_thresh)
                    hor_image(i,j-zeros_count:j-1)=1;
                else
                    one_flag=0;
                end
                zeros_count=0;
            end
            one_flag=1;
        else 
            if(one_flag==1)
                zeros_count=zeros_count+1;
            end
        end
    end
end

I tried to implement in C++ Code

    int hor_thres = 22;
                int one_count = 0;
                int zero_flag = 0;
                Mat tmpImg = Mat(Img.size(), CV_8UC1, Scalar(0, 0, 0));
                for (int j = 0; j<Img.rows; j++){
                    for (int i = 0; i<Img.cols; j++){
                        if (Img.at<uchar>(j, i) == 0)
                        {
                            if (zero_flag == 1)
                            {
                                if (one_count <= hor_thres)
                                {           
                                    tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));
// I want to do the same thing in Matlab as this  image(i,j-one_count:j-1)=0;
                                }
                                else
                                {
                                    zero_flag = 1;
                                }
                                one_count = 0;
                            }
                            zero_flag = 1;
                        }
                        else
                        {
                            if (zero_flag == 1)
                            {
                                one_count = one_count + 1;
                            }
                        }
                    }
                }

This time no error but the result is not expected ..

The issue is the way i want to write c++ code the same thing as

Matlab

tmpImg(i,j-one_count:j-1)=0;

C++

tmpText(cv::Range(j - zero_count, j), cv::Range(i, i+1)).setTo(cv::Scalar::all(255));

Anyidea???

Another thing is in Matlab the index start from 1 while C++ start from 0.

Thank