By chostDevil

2012-04-21 20:14:01 8 Comments

i want to detect logo inside image in order to remove it , i have an idea that's to look for objects which have the big number of pixels then remove , another idea is to loop through all the white pixels(i have inverted my image) and look for pixels which forms a large region and then remove this region, is there's any algorithm better that this one , also which methods in opencv will help me to detect object of large pixels number.


@Abid Rahman K 2012-04-22 09:18:02

I have a method to do this. I don't know whether this method applicable to all, but it works good here.

Below is code ( in Python ):

First convert image to grayscale, resize image, apply threshold, and make a mask image of same size and type of that of resized grayscale image. (Mask image is just a black image)

import cv2
import numpy as np

img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()
mask = np.zeros(gray.shape,np.uint8)

Now find contours in the threshold image. Filter the contour for area between 500 to 5000. It will be most probably a large white blob, obviously not letters. (Remember, this area is particular for this image. I dont know about your other images. You will have to find it yourself). Now draw this contour on the mask image filled with white color.

contours, hier = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if 200<cv2.contourArea(cnt)<5000:

Below is the detected contour image:

detected contour drawn on the input image

Next is the mask image:

New mask image

Now you invert image using cv2.bitwise_not function. There you have option for giving mask where we give our mask image so that function operates only on the area in input image where there is white in mask image.


And finally show the image :


And here is the result:

enter image description here


Above method is done to preserve "ORANGE" in white square. That is why some artifacts are there. If you don't want that orange also, it can be more accurate.

Just find the bounding rectangle for area-filtered contours and draw rectangle filled with black color.

Code :

import cv2
import numpy as np

img = cv2.imread('bus.png')
img = cv2.resize(img,(400,500))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,gray = cv2.threshold(gray,127,255,0)
gray2 = gray.copy()

contours, hier = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    if 200<cv2.contourArea(cnt)<5000:
        (x,y,w,h) = cv2.boundingRect(cnt)


Result :

detected bounding rects:

enter image description here

Then fillout those rectangles with black:

enter image description here

It is better than previous , of course if you don't want "ORANGE")

@chostDevil 2012-04-22 10:49:55

why did you resize the image?

@Abid Rahman K 2012-04-22 10:53:47

It is because your image is so big that my screen can't include it as a whole. (Not so important here. avoid it if you don't like it. Also smaller images means more faster the operations.) by the way, which method you actually wanted? with ORANGE or without ORANGE?

@chostDevil 2012-04-22 11:49:33

if you know c++ , please update your answer as i can't map between python and c++

@Abid Rahman K 2012-04-22 12:35:43

i am sorry, i dont know c++. But all functions are similar. Go to, and enter the python function in search box. You will get correspoding C++ function with complete docs. They have tutorials also. By the way, i am using opencv 2.4beta. It is compatible with 2.3 or 2.2 also.

@chostDevil 2012-04-22 13:27:35

ok thanks :) a lot

@sivann 2012-04-21 20:21:42

You may use morphological filters (perhaps alternating sequential filtering) to simplify your multi-color image and then use a segmentation algorithm like watershed or some granulometry method and choose the largest object. You may find several implementations online. But this will work only if the logo is discrete (e.g. not on the background)

@chostDevil 2012-04-21 20:34:10

what do you mean by ( not on the background)

@sivann 2012-04-21 20:38:18

@PatrickJones I mean if it's an image somewhere, and not one of those business cards where the logo is under the text and takes up the whole card. Or if the card is split into several color regions. There are many cases.

@chostDevil 2012-04-21 20:47:41

what's morphological filters or alternating sequential filtering please discuss

@sivann 2012-04-21 21:25:11

@PatrickJones morphological filters are connected filters, like dilation, erosion, opening, closing. ASF is using alternating opening and closing operators with different (larger each time) structuring element. Image analysis is a large and difficult topic, you could try to learn something from Mathwork's matlab demos: link

@sivann 2012-04-21 21:36:10

I see you posted the image. Filtering should be done before reducing color depth to avoid having noise (like the black "hair" inside the white square on top). After that, you could use something like this

@chostDevil 2012-04-21 21:41:00

what's this ?

@chostDevil 2012-04-21 21:52:13

this project has no files to download!

@Abid Rahman K 2012-04-22 09:32:14

Please, dont post big images directly. compress or resize before uploading. It is difficult to browse for low speed connections.

@sivann 2012-04-22 13:55:25

@PatrickJones it seems it disappeared. This project seems active though: link

@chostDevil 2012-04-23 21:36:13

@sivann what will be the case if the logo is inside the text or the card is split into different color regions?

@sivann 2012-04-24 17:35:31

@PatrickJones please provide example. There are too many cases, there is not one solution to cover them all.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

22 Answered Questions

[SOLVED] Peak detection in a 2D array

9 Answered Questions

[SOLVED] Representing and solving a maze given an image

4 Answered Questions

[SOLVED] Count pixels of a black and white image (OpenCV / C++ )

  • 2015-02-16 21:08:26
  • Jefferson
  • 1459 View
  • 0 Score
  • 4 Answer
  • Tags:   c++ opencv

0 Answered Questions

Remove de-focus region on image by opencv

4 Answered Questions

[SOLVED] Detecting clusters of white pixels in an image using openCV

1 Answered Questions

Detecting embedded blob regions in an image

2 Answered Questions

[SOLVED] OpenCV grouping white pixels

2 Answered Questions

[SOLVED] C++ and OpenCV: clustering white pixels algorithm

  • 2013-10-01 10:50:31
  • Pedro Batista
  • 2549 View
  • 2 Score
  • 2 Answer
  • Tags:   c++ algorithm opencv

0 Answered Questions

Sponsored Content