Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove watermark using python

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++];
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Python :: django form 
Python :: python list append 
Python :: for in print pyhton 
Python :: torch.stack 
Python :: how to declare a lambda function in python 
Python :: python lockfile 
Python :: Example of ceil method in python 
Python :: How to Replace substrings in python 
Python :: find location of max value in python list 
Python :: NumPy roll Syntax 
Python :: get maximum value index after groupby 
Python :: return function in python 
Python :: Python difference between filter and map 
Python :: adding new key in python 
Python :: literal_eval in python 
Python :: delete plotted text in python 
Python :: converting list of arrays with same size to single array python 
Python :: pandas find fifth caracter in field and change cell based on that number 
Python :: how to for loop in python stackoverflow 
Python :: python capitalize 
Python :: numpy diag() 
Python :: program to replace lower-case characters with upper-case and vice versa in python 
Python :: python - convert a list column into miltiple columns 
Python :: model checkpoint 
Python :: graph skewness detection 
Python :: tensorflow Dense layer activatity leaklyrelu 
Python :: how to count all files on linux 
Python :: Counter() Function 
Python :: list insert python 
Python :: loading bar in python 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =