Getting vision sensor image in python

Typically: "How do I... ", "How can I... " questions
Post Reply
Posts: 11
Joined: 16 Feb 2021, 21:47

Getting vision sensor image in python

Post by Usama1551 »

so I am using python to export the image from the vision sensor, i have it open using opencv since I want to process the image. I am having an issue we're the colors do not match up. so I have a blue concrete block infant of the camera and when I view it in python it is red, and it vice versa when the concrete block is red it shows up blue in python. all other colors like the floor and etc are matching. if anyone has a fix to this problem it would be great.

Code: Select all

import sim as vrep
import time
import cv2
import numpy as np
import imutils


clientID = vrep.simxStart('', 19999, True, True, 5000, 5)

red_lower = np.array([-10, 50, 50])
red_upper = np.array([10, 255, 255])

if clientID != -1:
    print('Connected to remote API server')
    print('Vision Sensor object handling')
    res, v1 = vrep.simxGetObjectHandle(clientID, 'vs1', vrep.simx_opmode_oneshot_wait)
    print('Getting first image')
    err, resolution, image = vrep.simxGetVisionSensorImage(clientID, v1, 0, vrep.simx_opmode_streaming)
    while vrep.simxGetConnectionId(clientID) != -1:
        err, resolution, image = vrep.simxGetVisionSensorImage(clientID, v1, 0, vrep.simx_opmode_buffer)
        if err == vrep.simx_return_ok:
            print("image OK!!!")

            img = np.array(image, dtype=np.uint8)
            img.resize([resolution[0], resolution[1], 3])
            img = imutils.rotate_bound(img, 180)
            cv2.imshow('image', img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
        elif err == vrep.simx_return_novalue_flag:
            print("no image yet")
    print("Failed to connect to remote API Server")


Posts: 722
Joined: 09 Sep 2013, 19:28

Re: Getting vision sensor image in python

Post by fferri »

If blue and red are swapped, probably you need to convert the colors with cv::cvtColor (passing COLOR_RGB2BGR).

You can learn more about why OpenCV uses BGR format (spoiler: due to historical reasons) searching "why OpenCV uses BGR" on Google.

Post Reply