Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

had problem when convert the image from BGR to HSV

Hi everyone,

I'm just a beginner in OpenCV. I'm trying to find the green ball's center point in the image. But when I try to convert the image from BGR to HSV the shows image result will be change to another kind of color instead of the orignal image. This is the result image after the convertion: image description

Base on my simple understand it should not show this image results after the BGR to HSV. So does anyone know what's the problem cause my problem? Thank you!

Follow is my whole program:

#!/usr/bin/env python
# coding=utf-8

import sys
import rospy
import cv2
#import cv
import argparse
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
import std_srvs.srv


def image_processing (color):
    #image_src = cv2.imread('home/ros_ws/src/baxter_examples/ColorBalls.jpg',0)

    print ("deciding color...............")
    if color == "blue":
        lower = (86,31,4)
        upper = (220,88,50)

    if color == "green":
        lower = (29,30,20)
        upper = (64,255,255)

    if color == "red":
        lower = (17,15,100)
        upper = (50,56,200)

    print color

    image_hsv = cv2.cvtColor(image_resized,cv2.COLOR_BGR2HSV)
    cv2.imshow("or_image",image_hsv)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    image_mask = cv2.inRange(image_hsv,lower,upper)
    #cv2.imshow("or_image",image_mask)
    #cv2.waitKey(0)

    image_mask = cv2.erode(image_mask, None, iterations=2)
    image_mask = cv2.dilate(image_mask, None, iterations=2)
    cv2.imshow("or_image",image_mask)
    cv2.waitKey(0)

    print ("end maskingg")
    print ("finding center.........")

    contours, hierarchy = cv2.findContours(image_mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    if contours  is None:
        print ("cont is None")
    print ("len.........")
    print  len(contours)

    if len(contours) > 0:
        print  ("############################")
        print  len(contours)

        c =  max (contours,key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        print center

        if radius > 1:
            cv2.circle(image_resized, (int(x), int(y)), int(radius),(0, 255, 255), 2)
            cv2.circle(image_resized, center, 2, (0, 0, 255), -1)

def main():
    global ball_color
    global image_src
    global image_resized

    arg_fmt = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__)

    parser.add_argument(
        '-c','---color', dest='color',choices=['blue','green','red'], required=True,
        help= "the ball color to pick up", 
         )
    args=parser.parse_args()

    print ("Initializing...........................................")
    rospy.init_node("ylj_ballposition",anonymous=True)

    #image_src = cv2.imread('/home/ros_ws/src/baxter_examples/scripts/ColorBalls.JPG')
    print ("reading the image")
    image_src = cv2.imread('Balls.JPG')
    print ("readed..............")

    if image_src is None:
        print ("the image read is None............")

    #print ("show the image............")
    #cv2.imshow("or_image",image_src)
    #cv2.waitKey(0)

    print image_src.shape
    print ("resizing........")
    image_resized = cv2.resize(image_src,(400,300),interpolation = cv2.INTER_AREA)
    print image_resized.shape
    print ("resized........")

    #cv2.imshow("or_image",image_resized)
    #cv2.waitKey(0)

    print ("start image processing......")
    image_processing(args.color)

    cv2.imshow("image_processed",image_resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__=='__main__':
    main()

had problem when convert the image from BGR to HSV

Hi everyone,

I'm just a beginner in OpenCV. I'm trying to find the green ball's center point in the image. But when I try to convert the image from BGR to HSV the shows image result will be change to another kind of color instead of the orignal image. This is the result image after the convertion: image description

image description

Base on my simple understand it should not show this image results after the BGR to HSV. So does anyone know what's the problem cause my problem? Thank you!

Follow is my whole program:

#!/usr/bin/env python
# coding=utf-8

import sys
import rospy
import cv2
#import cv
import argparse
import numpy as np
from std_msgs.msg import String
from sensor_msgs.msg import Image
import std_srvs.srv


def image_processing (color):
    #image_src = cv2.imread('home/ros_ws/src/baxter_examples/ColorBalls.jpg',0)

    print ("deciding color...............")
    if color == "blue":
        lower = (86,31,4)
        upper = (220,88,50)

    if color == "green":
        lower = (29,30,20)
        upper = (64,255,255)

    if color == "red":
        lower = (17,15,100)
        upper = (50,56,200)

    print color

    image_hsv = cv2.cvtColor(image_resized,cv2.COLOR_BGR2HSV)
    cv2.imshow("or_image",image_hsv)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    image_mask = cv2.inRange(image_hsv,lower,upper)
    #cv2.imshow("or_image",image_mask)
    #cv2.waitKey(0)

    image_mask = cv2.erode(image_mask, None, iterations=2)
    image_mask = cv2.dilate(image_mask, None, iterations=2)
    cv2.imshow("or_image",image_mask)
    cv2.waitKey(0)

    print ("end maskingg")
    print ("finding center.........")

    contours, hierarchy = cv2.findContours(image_mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    if contours  is None:
        print ("cont is None")
    print ("len.........")
    print  len(contours)

    if len(contours) > 0:
        print  ("############################")
        print  len(contours)

        c =  max (contours,key=cv2.contourArea)
        ((x, y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))

        print center

        if radius > 1:
            cv2.circle(image_resized, (int(x), int(y)), int(radius),(0, 255, 255), 2)
            cv2.circle(image_resized, center, 2, (0, 0, 255), -1)

def main():
    global ball_color
    global image_src
    global image_resized

    arg_fmt = argparse.RawDescriptionHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=arg_fmt, description=main.__doc__)

    parser.add_argument(
        '-c','---color', dest='color',choices=['blue','green','red'], required=True,
        help= "the ball color to pick up", 
         )
    args=parser.parse_args()

    print ("Initializing...........................................")
    rospy.init_node("ylj_ballposition",anonymous=True)

    #image_src = cv2.imread('/home/ros_ws/src/baxter_examples/scripts/ColorBalls.JPG')
    print ("reading the image")
    image_src = cv2.imread('Balls.JPG')
    print ("readed..............")

    if image_src is None:
        print ("the image read is None............")

    #print ("show the image............")
    #cv2.imshow("or_image",image_src)
    #cv2.waitKey(0)

    print image_src.shape
    print ("resizing........")
    image_resized = cv2.resize(image_src,(400,300),interpolation = cv2.INTER_AREA)
    print image_resized.shape
    print ("resized........")

    #cv2.imshow("or_image",image_resized)
    #cv2.waitKey(0)

    print ("start image processing......")
    image_processing(args.color)

    cv2.imshow("image_processed",image_resized)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__=='__main__':
    main()