Ask Your Question
1

how can i discover the values of Y,cb,cr on one pixel (picture) with python

asked 2018-05-09 16:00:17 -0600

Toutou gravatar image

I am doing a project with image processing with opencv. I am stuck in this stage and thanks in advance

edit retag flag offensive close merge delete

Comments

The Wikipedia page on Y Cb Cr has many formulas for converting from RGB.

https://en.wikipedia.org/wiki/YCbCr#I...

Is this what you're looking for?

sjhalayka gravatar imagesjhalayka ( 2018-05-09 16:12:33 -0600 )edit

i don't understand how can i write this in python with pixel for exemple x = img[360,792]
print(x) ===> [125 122 100] how can i have the values now of Y , cb , cr of this pixel ?

Toutou gravatar imageToutou ( 2018-05-09 16:15:29 -0600 )edit

R = 255 G = 127 B = 0

Y =   16 +  65.738*R/256 + 129.057*G/256 +  25.064*B/256
Cb = 128 -  37.945*R/256 -  74.494*G/256 + 112.439*B/256
Cr = 128 + 112.439*R/256 -  94.154*G/256 -  18.285*B/256

print(Y)
print(Cb)
print(Cr)
sjhalayka gravatar imagesjhalayka ( 2018-05-09 16:33:54 -0600 )edit

thanks my friend but how can program konw the values R G B with this function ?

Toutou gravatar imageToutou ( 2018-05-09 16:39:03 -0600 )edit
1

Try this code:

import cv2
import numpy

frame = cv2.imread('sparks.png')

if frame is None:
    print('Error loading image')
    exit()

rows = frame.shape[0]
cols = frame.shape[1]

for i in range(0, rows):
    for j in range(0, cols):
        R = frame[i, j][2]
        G = frame[i, j][1]
        B = frame[i, j][0]

        Y =   16 +  65.738*R/256 + 129.057*G/256 +  25.064*B/256
        Cb = 128 -  37.945*R/256 -  74.494*G/256 + 112.439*B/256
        Cr = 128 + 112.439*R/256 -  94.154*G/256 -  18.285*B/256
sjhalayka gravatar imagesjhalayka ( 2018-05-09 16:54:49 -0600 )edit
1

thanks my friend :)

Toutou gravatar imageToutou ( 2018-05-09 16:56:58 -0600 )edit

Pas de problème. :D

sjhalayka gravatar imagesjhalayka ( 2018-05-09 17:10:48 -0600 )edit
1

and cvtcolor with parameter cv.COLOR_BGR2YCrCb?

LBerger gravatar imageLBerger ( 2018-05-10 04:19:04 -0600 )edit

@LBerger -- Thanks for pointing that out.

sjhalayka gravatar imagesjhalayka ( 2018-05-10 10:26:27 -0600 )edit

problem (error syntax ) : in loop i , j
for i in range(0, rows): for j in range(0, cols): R = frame[i, j][2] G = frame[i, j][1] B = frame[i, j][0]

Toutou gravatar imageToutou ( 2018-05-10 14:46:28 -0600 )edit

1 answer

Sort by » oldest newest most voted
2

answered 2018-05-09 17:28:10 -0600

sjhalayka gravatar image

updated 2018-05-10 14:22:56 -0600

Please mark my answer as correct (click the check mark), and please make sure to give me an upvote. :)

image description

import cv2
import numpy

frame = cv2.imread('sparks.png')

if frame is None:
    print('Error loading image')
    exit()

rows = frame.shape[0]
cols = frame.shape[1]

for i in range(0, rows):
    for j in range(0, cols):
        R = frame[i, j][2]
        G = frame[i, j][1]
        B = frame[i, j][0]

        Y =   16 +  65.738*R/256 + 129.057*G/256 +  25.064*B/256
        Cb = 128 -  37.945*R/256 -  74.494*G/256 + 112.439*B/256
        Cr = 128 + 112.439*R/256 -  94.154*G/256 -  18.285*B/256

Thanks to LBerger for this simpler OpenCV code:

ycrcb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2YCrCb)

... this is not to say that OpenCV uses the same constants as used here in the manual approach. I think it's a matter of looking into the OpenCV source code.

edit flag offensive delete link more

Comments

1

@sjhalayka and @Toutou if you want to extract R,G,B or any other layers from an image. Use this B,G,R=image[...,0],image[...,1],image[...,2] instead of a loop its much faster

Santhosh1 gravatar imageSanthosh1 ( 2018-05-10 23:44:54 -0600 )edit

@Santhosh1 -- Can you please put that in a more comprehensive code, perhaps as your own answer (I'll upvote it)? I'm not quite sure I get it.

sjhalayka gravatar imagesjhalayka ( 2018-05-11 10:00:20 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-09 16:00:17 -0600

Seen: 2,332 times

Last updated: May 10 '18