Detect color in the middle of image (OpenCV, Python)

shape and color detection -- opencv
find dominant color in image python
detect yellow color opencv python
detect black color opencv python
multiple color detection opencv python
opencv colors
opencv remove color from image python
skin color detection opencv-python

I have these 3 images (consider "image" the whole square not only the figure inside - this is just for demonstration purposes):

What I want to do is detect the colour in the middle (center) of each one. So, having an area (square or circle) in the center and with OpenCV detect which is the colour. Something like a color picker...

The purpose is to have 3 values, 3 for each image (BGR).


What is the colour in the ROI ?



Using this code I can find the middle of an image and apply a mask.

import cv2
import numpy as np

img = cv2.imread("im2.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask =, (int(width / 2), int(height / 2)), 20, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

cv2.imshow("masked", masked_img)

Now it remains to find BGR of the masked area (visible one...)

import cv2
import numpy as np

img = cv2.imread('image.png')
height, width = img.shape[:2]
# Change these values to fit the size of your region of interest
roi_size = 10 # (10x10)
roi_values = img[(height-roi_size)/2:(height+roi_size)/2,(width-roi_size)/2:(width+roi_size)/2]
mean_blue = np.mean(roi_values[:,:,0])
mean_green = np.mean(roi_values[:,:,1])
mean_red = np.mean(roi_values[:,:,2])

print("R: {}  G: {}  B: {}").format(mean_red, mean_green, mean_blue)  

Determining object color with OpenCV, This is the final post in our three part series on shape detection and and determine the color of objects in images using Python and OpenCV. of the contours on Line 38, while Lines 40-42 compute the center of the shape. This article will help in color detection in Python using OpenCV through both videos and saved images. So let’s start learning how to detect color using OpenCV in Python. Firstly set up the python environment and make sure that OpenCV and NumPy are being installed on your PC as NumPy is also a need for working with OpenCV.

Make sure the image is a PhotoImage, calculate middle point and fetch the colours:

r,g,b = image.get(x, y)

Image Segmentation Using Color Spaces in OpenCV + Python , by Rebecca Stone 23 Comments intermediate machine-learning Color Spaces and Reading Images in OpenCV; Visualizing Nemo in RGB Color Space Free Bonus: Click here to get the Python Face Detection & OpenCV Examples  I am trying to detect red color from the video that's being taken from my webcam. The following code example given below is taken from OpenCV Documentation. The code is given below: The line lower_blue = np.array([110,50,50]) has the lower range Blue HSV value and the line upper_blue = np.array([130,255,255]) has the higher range Blue HSV value.

Using Pillow:

from PIL import Image

im =

width, height = im.size #get image size

#since you do not specify the size of the center area in the question, just replace the below variables with what you want
left = (width - your_width)/2
top = (height - your_height)/2
right = (width + your_width)/2
bottom = (height + your_height)/2

im.crop((left, top, right, bottom)) #crop the center of the image

rgb = im.convert('RGB') # get three R G B values
r, g, b = rgb.getpixel((1, 1))


With OpenCV replace the two first lines with:

im = cv2.imread(path_to_image)
height, width = im.shape[:2]

Color Identification in Images, This is because, by default, OpenCV reads image in the sequence Blue Next, we define a method that will help us get an image into Python in As we divided each color by 255 before, we now multiply it by 255 again while finding the colors​. A Medium publication sharing concepts, ideas, and codes. Figure 4: Detecting gray in an image using OpenCV and Python. Summary. In this blog post I showed you how to perform color detection using OpenCV and Python. To detect colors in images, the first thing you need to do is define the upper and lower limits for your pixel values.

You have an image of size w x h. Let's assume a roi size of 11 pixel per side, so offset = 5

Access the pixel from (w/2 - width/2, h/2 - height/2) to (w/2 + width/2, h/2 + height/2).

Then calculate the mean of all extracted pixel (so you're more robust to color variation).

You can of course change the color space if you want it in other space color depending of the kind of picture are you going to analyze.

Object detection via color-based image segmentation using python, Object detection via color-based image segmentation using python. A tutorial on contouring using python & OpenCV. can get the index of the leaf contour in the contours array, from that we get the area and center of the leaf. In this tutorial I will be showing how you can detect and track a particular colour using Python & OpenCV. NOTE :- For this you will need basic knowledge of python. So lets get started. Step 1: INSTALLING PYTHON :-First step is to install python in your computer.

This will find the RGB value of the pixel in the center of image using OpenCV.

import cv2
import numpy as np

img = cv2.imread("image.png")

height, width, depth = img.shape
circle_img = np.zeros((height, width), np.uint8)

mask =, (int(width / 2), int(height / 2)), 1, 1, thickness=-1)
masked_img = cv2.bitwise_and(img, img, mask=circle_img)

circle_locations = mask == 1
bgr = img[circle_locations]

rgb = bgr[..., ::-1]



Color of single shape detected,how to detect , Hi, Iam trying on Python to detect each shape colors.Code bellow COLOR_BGR2GRAY) cv2.imshow('HSV Image',gray) cv2.waitKey(0) CHAIN_APPROX_SIMPLE)[-2] center = None if len(cnts) > 0: c = max(cnts, key=​cv2. The following code in python uses OpenCV library which is employed for image processing techniques. The program allows the detection of a specific color in a livestream video content. A video is composed of infinite frames at different time instants. We will detect the colour of every frame one by one.

Python Image Processing Tutorial (Using OpenCV), To find the center of an image, the first step is to We can use the cvtColor() method of cv2 as we did before. the size, the color that we want the circle to be and the thickness. How to Detect Contours in Images using OpenCV in Python Learning how to detect contours in images for image segmentation, shape analysis and object detection and recognition using OpenCV in Python.

Basic Image Processing In Python, Basic overview of image processing in Python. assumes the image to be BGR or BGRA (BGR is the default OpenCV colour format). So, if we see the shape of both low_pixel and pic , we'll find that both have the same shape. total_col/2 ''' Measure distance value from center to each border pixel. Image Processing. This repository is for image processing. You will find two scripts. One is In this script you have to complete the given code template and draw contours around each of the RGB colors. The second script is shape detection. In this you have to complete the given code template to detect the circle and square shape.

Finding the dominant colors of an image, You specify a range of colors, then use OpenCV to identify regions in an image that be to take an average of the colors to find the midpoint of his range of colors. Find the average color in an image # import cv2 import numpy as np img python Bar 1 RGB values: (131, 114, 99) HSV  OpenCV Color Detection and filtering with python. Run the code below with the Python Idle application on either the Raspberry Pi or the Windows desktop. The windows should appear on the desktop like in the above image. You can operate the HSV (Hue, Saturation, Value) sliders to isolate the colour you want to detect in the image.

  • mean the pixel value of each pixel in your roi
  • What ? Can you explain more ? I thought about masks, histograms and hsv but what do you mean by "pixel value of each pixel" ? Meantime, thanks.
  • Histograms does not give you any information about "position", so is useless in what you want to do. I'm going to add more.
  • @Link it's not alright to take an answer on your own question, modify it, and post it as your own.
  • He tagged "opencv".
  • @Moia it's easy to transform it though, plus PIL might be a better option for this