Home > Mobile >  OpenCV Python :
OpenCV Python :

Time:08-25

I am trying to implement a color correction algorithm in python using this c code as a reference : https://docs.opencv.org/4.x/d1/dc1/tutorial_ccm_color_correction_model.html

Here is the code so far :

import cv2

chartype = cv2.mcc.MCC24
image = cv2.imread("image.jpg",cv2.IMREAD_COLOR)

if image is None:
    {
        print("Invalid Image ! \n")
    }

imageCopy = image.copy()
detector = cv2.mcc.CCheckerDetector.create()
# Marker type to detect
if(not detector.process(image,chartype,1)) :
    {
        print("ChartColor not detected \n")
    }

checkers = detector.getListColorChecker()
for checker in checkers :
    cdraw = cv2.mcc.CCheckerDraw.create(checker)
    cdraw.draw(image)
    chartsRGB = checker.getChartsRGB()
    src = chartsRGB[:,1].copy().reshape(3,chartsRGB.rows/3)

I face a problem with this line : src = chartsRGB[:,1].copy().reshape(3,chartsRGB.rows/3) It returns AttributeError: 'numpy.ndarray' object has no attribute 'rows' However, the getChartsRGB() function is supposed to return a cv2.Map and not a numpy.ndarray I also tried to cast chartsRGB into a cv2.Map (cv2.Map(chartsRGB)) but I get the following error messages:

AttributeError: 'Mat' object has no attribute 'col'
AttributeError: 'Mat' object has no attribute 'rows'

Could anyone help ?

CodePudding user response:

I guess if you are trying to use the webcam. Please add this line of code cap = cv2.VideoCapture(0)

and apparently img is None due to no data read.

Change to:

while True:
    success, img = cap.read()
    if img is None:
        break
    imgResult = img.copy()

here is the edited code for reference,

import cv2

cap = cv2.VideoCapture(0)
chartype = cv2.mcc.MCC24
image = cv2.imread("image.jpg",cv2.IMREAD_COLOR)

while True:
    success, img = cap.read()
    if image is None:
        {
            print("Invalid Image ! \n")
        }
        break

    imageCopy = image.copy()
    detector = cv2.mcc.CCheckerDetector.create()
    # Marker type to detect
    if(not detector.process(image,chartype,1)) :
        {
            print("ChartColor not detected \n")
        }

    checkers = detector.getListColorChecker()
    for checker in checkers :
        cdraw = cv2.mcc.CCheckerDraw.create(checker)
        cdraw.draw(image)
        chartsRGB = checker.getChartsRGB()
        src = chartsRGB[:,1].copy().reshape(3,chartsRGB.rows/3)

CodePudding user response:

Thank you everyone for your comments. I managed to find the exact answer for this post here : Converting Color Correction opencv module example from C to python

  • Related