ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

using OpenCV to do image process

asked 2016-07-27 03:00:03 -0500

Zero gravatar image

Hi everyone,

I'm trying to use the basic OpenCV to find the choose color ball's center x,y value from the image. But When I try to run my program there had some error happen but I had no idea what's wrong in my program. Can anyone help me?

Follow is my progam:

#!/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,86,6)
        upper = (64,255,255)

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

    print color

    image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)##################
    image_mask = cv2.inRange(image_hsv,lower,upper)

    image_mask = cv2.erode(image_mask, None, iterations=2)
    image_mask = cv2.dilate(image_mask, None, iterations=2)

    print ("end maskingg")

    print ("finding center.........")

    contours,hierarchy = cv2.findContours(image_mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]

    if len(contours) > 0:

        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_src, (int(x), int(y)), int(radius),(0, 255, 255), 2)

            cv2.circle(image_src, center, 2, (0, 0, 255), -1)

def main():
    global ball_color
    global image_src

    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/ColorBalls.jpg',0)

    image_processing(args.color)

    cv2.imshow("image_processed",image_src)

if __name__=='__main__':
    main()

This is the error message show:

Initializing...........................................
deciding color...............
blue
OpenCV Error: Assertion failed ((scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F)) in cvtColor, file /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp, line 3959
Traceback (most recent call last):
  File "/home/leddar/ros_ws/src/baxter_examples/scripts/ylj/ylj_imgball.py", line 101, in <module>
    main()
  File "/home/leddar/ros_ws/src/baxter_examples/scripts/ylj/ylj_imgball.py", line 91, in main
    image_processing(args.color)
  File "/home/leddar/ros_ws/src/baxter_examples/scripts/ylj/ylj_imgball.py", line 36, in image_processing
    image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)##################
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/color.cpp:3959: error: (-215) (scn == 3 || scn == 4) && (depth == CV_8U || depth == CV_32F) in function cvtColor

Thank you.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-07-27 03:22:20 -0500

NEngelhard gravatar image

This is an OpenCV question (-> answers.opencv.org ). But you try to convert a grayscale image using BGR2HSV which obviously does not work. The '0' in your cv2.imread asks specifically for a gray scale image, so just use '1' (aka cv2.IMREAD_COLOR) to start with a color image.

http://docs.opencv.org/3.0-beta/doc/p...

edit flag offensive delete link more

Comments

Oh....Sorry, I didn't know that this question had to ask in answers.opencv.org . But thank you for answering my question.

Zero gravatar image Zero  ( 2016-07-27 03:39:45 -0500 )edit

Hi, When I changed the imread's 0 to 1 it still shows the same error. It's that mean the imread part was still wrong?

Zero gravatar image Zero  ( 2016-07-27 03:49:33 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2016-07-27 03:00:03 -0500

Seen: 1,216 times

Last updated: Jul 27 '16