61

First of all I have this image and I want to make an application that can detect images like it and remove the circle (watermark) from it.

image has a watermark

int main(){
    Mat im1,im2,im3,gray,gray2,result;

    im2=imread(" (2).jpg");
    namedWindow("x",CV_WINDOW_FREERATIO);
    imshow("x",im2);

    //converting it to gray
    cvtColor(im2,gray,CV_BGR2GRAY);
    // creating a new image that will have the cropped ellipse
    Mat ElipseImg(im2.rows,im2.cols,CV_8UC1,Scalar(0,0,0));

    //detecting the largest circle
    GaussianBlur(gray,gray,Size(5,5),0);
    vector<Vec3f> circles;
    HoughCircles(gray,circles,CV_HOUGH_GRADIENT,1,gray.rows/8,100,100,100,0);

    uchar x;
    int measure=0;int id=0;
    for(int i=0;i<circles.size();i++){
        if(cvRound(circles[i][2])>measure && cvRound(circles[i][2])<1000){
            measure=cvRound(circles[i][2]);
            id=i;
        }
    }


    Point center(cvRound(circles[id][0]),cvRound(circles[id][1]));
    int radius=cvRound(circles[id][2]);
    circle(im2,center,3,Scalar(0,255,0),-1,8,0);
    circle(im2,center,radius,Scalar(0,255,0),2,8,0);
    ellipse(ElipseImg,center,Size(radius,radius),0,0,360,Scalar(255,255,255),-1,8);
    cout<<"center: "<<center<<" radius: "<<radius<<endl;



    Mat res;
    bitwise_and(gray,ElipseImg,result);
    namedWindow("bitwise and",CV_WINDOW_FREERATIO);
    imshow("bitwise and",result);

    // trying to estimate the Intensity  of the circle for the thresholding
    x=result.at<uchar>(cvRound(circles[id][0]+30),cvRound(circles[id][1]));
    cout<<(int)x;

    //thresholding the  output image
    threshold(ElipseImg,ElipseImg,(int)x-10,250,CV_THRESH_BINARY);
    namedWindow("threshold",CV_WINDOW_FREERATIO);
    imshow("threshold",ElipseImg);

    // making bitwise_or
    bitwise_or(gray,ElipseImg,res);
    namedWindow("bitwise or",CV_WINDOW_FREERATIO);
    imshow("bitwise or",res);

    waitKey(0);
}

So far what I made is:

  1. I convert it to grayscale
  2. I detect the largest circle using Hough circles and then make a circle with same radius in a new image
  3. This new circle with the gray-scaled one using (bitwise_and) gives me an image with only that circle
  4. Threshold that new image
  5. bitwise_or the result of the threshold

My problem is that any black text on the curved white line inside this circle didn't appear. I tried to remove the color by using the pixel values instead of threshold, but the problem is the same. So any solutions or suggestions?

These are the results: enter image description here

Joel G
  • 25
  • 2
  • 7
Ahmed Ramzy
  • 839
  • 1
  • 11
  • 18

2 Answers2

48

I'm not sure if the following solution is acceptable in your case. But I think it performs slightly better, and doesn't care about the shape of the watermark.

  • Remove the strokes using morphological filtering. This should give you a background image. background

  • Calculate the difference image: difference = background - initial, and threshold it: binary = threshold(difference)

binary1

  • Threshold the background image and extract the dark region covered by the watermark

dark

  • From the initial image, extract pixels within the watermark region and threshold these pixels, then paste them to the earlier binary image

binary2

Above is a rough description. Code below should explain it better.

Mat im = [load the color image here];

Mat gr, bg, bw, dark;

cvtColor(im, gr, CV_BGR2GRAY);

// approximate the background
bg = gr.clone();
for (int r = 1; r < 5; r++)
{
    Mat kernel2 = getStructuringElement(MORPH_ELLIPSE, Size(2*r+1, 2*r+1));
    morphologyEx(bg, bg, CV_MOP_CLOSE, kernel2);
    morphologyEx(bg, bg, CV_MOP_OPEN, kernel2);
}

// difference = background - initial
Mat dif = bg - gr;
// threshold the difference image so we get dark letters
threshold(dif, bw, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
// threshold the background image so we get dark region
threshold(bg, dark, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

// extract pixels in the dark region
vector<unsigned char> darkpix(countNonZero(dark));
int index = 0;
for (int r = 0; r < dark.rows; r++)
{
    for (int c = 0; c < dark.cols; c++)
    {
        if (dark.at<unsigned char>(r, c))
        {
            darkpix[index++] = gr.at<unsigned char>(r, c);
        }
    }
}
// threshold the dark region so we get the darker pixels inside it
threshold(darkpix, darkpix, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

// paste the extracted darker pixels
index = 0;
for (int r = 0; r < dark.rows; r++)
{
    for (int c = 0; c < dark.cols; c++)
    {
        if (dark.at<unsigned char>(r, c))
        {
            bw.at<unsigned char>(r, c) = darkpix[index++];
        }
    }
}
dhanushka
  • 10,492
  • 2
  • 37
  • 47
  • Awesome , it worked very well but i have problem with darker pages - darker watermark - it just copy the whole watermark to the bw image so it's like nothing done at the end , how can i deal with something like that ? – Ahmed Ramzy Aug 21 '15 at 15:39
  • 2
    Check the intermediate images: the **difference**, **background**, the **watermark mask** and the **intermediate binary**. Here we use Otsu method, so the images subjected to thresholding had better be bimodal. You can check if the letters inside the watermark are segmented as expected by cropping a part of watermark that contains text and applying Otsu thresholding to it. It could also be a matter of CV_THRESH_BINARY vs CV_THRESH_BINARY_INV. – dhanushka Aug 22 '15 at 10:42
  • @dhanushka Please Can someone help with the Java Code for the vector Looping Part.I cannot find something equivalent in Java. I posted Question here --> http://answers.opencv.org/question/130997/how-to-parse-a-binary-image-pixel-by-pixel-in-java-open-cv/ – Vishal Nair Mar 02 '17 at 21:30
  • @VishalNair I'm not very familiar with opencv java interface. But a simple google search pointed [here](http://stackoverflow.com/a/15721754/2571705) and [here](http://answers.opencv.org/question/5/how-to-get-and-modify-the-pixel-of-mat-in-java/?answer=8#post-id-8). So, basically you can use `Mat::get` and `Mat::put` methods. The `darkpix` will have to be an opencv `Mat` if java interface does not operate on java vectors. – dhanushka Mar 03 '17 at 02:33
  • @dhanushka yes I already did that simple google search :).Problem was not that. Maybe I didn't communicated properly.Problem was with the unsigned char portion :) .. thanks for helping out anyways ! Figured out the solutn... cheers ! – Vishal Nair Mar 03 '17 at 12:39
14

A Python version of dhanushka's answer

# Import the necessary packages
import cv2
import numpy as np


def back_rm(filename):
    # Load the image
    img = cv2.imread(filename)

    # Convert the image to grayscale
    gr = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Make a copy of the grayscale image
    bg = gr.copy()

    # Apply morphological transformations
    for i in range(5):
        kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                            (2 * i + 1, 2 * i + 1))
        bg = cv2.morphologyEx(bg, cv2.MORPH_CLOSE, kernel2)
        bg = cv2.morphologyEx(bg, cv2.MORPH_OPEN, kernel2)

    # Subtract the grayscale image from its processed copy
    dif = cv2.subtract(bg, gr)

    # Apply thresholding
    bw = cv2.threshold(dif, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
    dark = cv2.threshold(bg, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

    # Extract pixels in the dark region
    darkpix = gr[np.where(dark > 0)]

    # Threshold the dark region to get the darker pixels inside it
    darkpix = cv2.threshold(darkpix, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    # Paste the extracted darker pixels in the watermark region
    bw[np.where(dark > 0)] = darkpix.T

    cv2.imwrite('final.jpg', bw)


back_rm('watermark.jpg')

Here is the final result:
The processing time is very short using numpy

time python back_rm.py 

real    0m0.391s
user    0m0.518s
sys     0m0.185s

enter image description here

singrium
  • 2,746
  • 5
  • 32
  • 45