1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Adding macbeth chart detector to objdetect module from opencv_contrib (#26906)

* Added mcc to opencv modules

* Removed color correction module

* Updated parameters return type

* Added python sample for macbeth_chart_detection

* Added models.yml support to samples

* Removed unnecessary headers and classes

* fixed datatype conversion

* fixed datatype conversion

* Cleaned headers and added reference/actual colors to samples

* Added mcc tutorial

* fixed datatype and header

* replaced unsigned with int

* Aligned actual and reference color function, added imread

* Fixed shadow variable

* Updated samples

* Added last frame colors prints

* updated detector class

* Added getter functions and useNet function

* Refactoring

* Fixes in test

* fixed infinite divison issue
This commit is contained in:
Gursimar Singh
2025-03-28 13:43:30 +05:30
committed by GitHub
parent a674ae1bce
commit 69b91cabb4
31 changed files with 5754 additions and 0 deletions
+194
View File
@@ -0,0 +1,194 @@
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
#include "../dnn/common.hpp"
using namespace std;
using namespace cv;
using namespace cv::dnn;
using namespace mcc;
const string about =
"This sample demonstrates mcc checker detection with DNN based model and thresholding (default) techniques.\n\n"
"To run default:\n"
"\t ./example_cpp_macbeth_chart_detection --input=path/to/your/input/image/or/video (don't give --input flag if want to use device camera)\n"
"With DNN model:\n"
"\t ./example_cpp_macbeth_chart_detection mcc --input=path/to/your/input/image/or/video\n\n"
"Model path can also be specified using --model argument. And config path can be specified using --config. Download it using python download_models.py mcc from dnn samples directory\n\n";
const string param_keys =
"{ help h | | Print help message. }"
"{ @alias | | An alias name of model to extract preprocessing parameters from models.yml file. }"
"{ zoo | ../dnn/models.yml | An optional path to file with preprocessing parameters }"
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
"{ type | 0 | chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0 }"
"{ num_charts | 1 | Maximum number of charts in the image }"
"{ model | | Path to the model file for using dnn model. }";
const string backend_keys = format(
"{ backend | default | Choose one of computation backends: "
"default: automatically (by default), "
"openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"opencv: OpenCV implementation, "
"vkcom: VKCOM, "
"cuda: CUDA, "
"webnn: WebNN }");
const string target_keys = format(
"{ target | cpu | Choose one of target computation devices: "
"cpu: CPU target (by default), "
"opencl: OpenCL, "
"opencl_fp16: OpenCL fp16 (half-float precision), "
"vpu: VPU, "
"vulkan: Vulkan, "
"cuda: CUDA, "
"cuda_fp16: CUDA fp16 (half-float preprocess) }");
string keys = param_keys + backend_keys + target_keys;
static bool processFrame(const Mat& frame, Ptr<CCheckerDetector> detector, Mat& src, Mat& tgt, int nc){
Mat imageCopy = frame.clone();
if (!detector->process(frame, nc))
{
return false;
}
vector<Ptr<CChecker>> checkers = detector->getListColorChecker();
detector->draw(checkers, frame);
src = checkers[0]->getChartsRGB(false);
tgt = detector->getRefColors();
imshow("Image result", frame);
imshow("Original", imageCopy);
return true;
}
int main(int argc, char *argv[])
{
CommandLineParser parser(argc, argv, keys);
parser.about(about);
if (parser.has("help"))
{
cout << about << endl;
parser.printMessage();
return -1;
}
string modelName = parser.get<String>("@alias");
string zooFile = parser.get<String>("zoo");
const char* path = getenv("OPENCV_SAMPLES_DATA_PATH");
if ((path != NULL) || parser.has("@alias") || (parser.get<String>("model") != "")) {
zooFile = findFile(zooFile);
}
else{
cout<<"[WARN] set the environment variables or pass the arguments --model, --config and models.yml file using --zoo for using dnn based detector. Continuing with default detector.\n\n";
}
keys += genPreprocArguments(modelName, zooFile);
parser = CommandLineParser(argc, argv, keys);
int t = parser.get<int>("type");
CV_Assert(0 <= t && t <= 2);
ColorChart chartType = ColorChart(t);
const string sha1 = parser.get<String>("sha1");
const string model_path = findModel(parser.get<string>("model"), sha1);
const string config_sha1 = parser.get<String>("config_sha1");
const string pbtxt_path = findModel(parser.get<string>("config"), config_sha1);
const string backend = parser.get<String>("backend");
const string target = parser.get<String>("target");
int nc = parser.get<int>("num_charts");
Ptr<CCheckerDetector> detector;
if (model_path != "" && pbtxt_path != ""){
EngineType engine = ENGINE_AUTO;
if (backend != "default" || target != "cpu"){
engine = ENGINE_CLASSIC;
}
Net net = readNetFromTensorflow(model_path, pbtxt_path, engine);
net.setPreferableBackend(getBackendID(backend));
net.setPreferableTarget(getTargetID(target));
detector = CCheckerDetector::create(net);
cout<<"Detecting checkers using neural network."<<endl;
}
else{
detector = CCheckerDetector::create();
}
detector->setColorChartType(chartType);
bool isVideo = true;
Mat image;
VideoCapture cap;
if (parser.has("input")){
const string inputFile = parser.get<String>("input");
image = imread(findFile(inputFile));
if (!image.empty())
{
isVideo = false;
}
else
{
// Not an image, so try opening it as a video.
cap.open(findFile(inputFile));
if (!cap.isOpened())
{
cout << "[ERROR] Could not open file as an image or video: " << inputFile << endl;
return -1;
}
}
}
else
cap.open(0);
Mat src, tgt;
bool found = false;
if (isVideo){
cout<<"To print the actual colors and reference colors for current frame press SPACEBAR. To resume press SPACEBAR again"<<endl;
while (cap.grab())
{
Mat frame;
cap.retrieve(frame);
found = processFrame(frame, detector, src, tgt, nc);
int key = waitKey(10);
if (key == ' '){
if(found){
cout<<"Reference colors: "<<tgt<<endl<<"--------------------"<<endl;
cout<<"Actual colors: "<<src<<endl<<endl;
cout<<"Press spacebar to resume."<<endl;
waitKey(0);
cout << "Resumed! Processing continues..." << endl;
}
else{
cout<<"No color chart detected!!"<<endl;
}
}
else if (key == 27) exit(0);
}
if(found){
cout<<"Reference colors: "<<tgt<<endl<<"--------------------"<<endl;
cout<<"Actual colors: "<<src<<endl<<endl;
}
}
else{
found = processFrame(image, detector, src, tgt, nc);
if(found){
cout<<"Reference colors: "<<tgt<<endl<<"--------------------"<<endl;
cout<<"Actual colors: "<<src<<endl<<endl;
waitKey(0);
}
else{
cout<<"No chart detected!!"<<endl;
}
}
return 0;
}
+15
View File
@@ -477,3 +477,18 @@ ldm_inpainting:
height: 512
rgb: true
sample: "ldm_inpainting"
################################################################################
# Macbeth chart detection model.
################################################################################
mcc:
load_info:
url: "https://github.com/gursimarsingh/opencv_zoo/raw/refs/heads/mcc_model/models/macbeth_chart_detector/frozen_inference_graph.pb?download="
sha1: "fae7dbef14c4ae1fca76f3662220fbd460ed5ed6"
model: "frozen_inference_graph.pb"
config_load_info:
url: "https://github.com/gursimarsingh/opencv_zoo/raw/refs/heads/mcc_model/models/macbeth_chart_detector/graph.pbtxt?download="
sha1: "8350cb8f078ecefa1cd566e89930ede25a192310"
config: "graph.pbtxt"
sample: "mcc"
+155
View File
@@ -0,0 +1,155 @@
import cv2 as cv
import numpy as np
import argparse
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from dnn.common import *
def get_args_parser(func_args):
backends = ("default", "openvino", "opencv", "vkcom", "cuda")
targets = ("cpu", "opencl", "opencl_fp16", "ncs2_vpu", "hddl_vpu", "vulkan", "cuda", "cuda_fp16")
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--zoo', default=os.path.join(os.path.dirname(os.path.abspath(__file__)), '../dnn', 'models.yml'),
help='An optional path to file with preprocessing parameters.')
parser.add_argument('--input', help='Path to input image or video file. Skip this argument to capture frames from a camera.', default=0, required=False)
parser.add_argument('--method', help='choose method: dexined or canny', default='canny', required=False)
parser.add_argument('--chart_type', type=int, default=0,
help='chartType: 0-Standard, 1-DigitalSG, 2-Vinyl, default:0')
parser.add_argument('--num_charts', type=int, default=1,
help='Maximum number of charts in the image')
parser.add_argument('--backend', default="default", type=str, choices=backends,
help="Choose one of computation backends: "
"default: automatically (by default), "
"openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
"opencv: OpenCV implementation, "
"vkcom: VKCOM, "
"cuda: CUDA, "
"webnn: WebNN")
parser.add_argument('--target', default="cpu", type=str, choices=targets,
help="Choose one of target computation devices: "
"cpu: CPU target (by default), "
"opencl: OpenCL, "
"opencl_fp16: OpenCL fp16 (half-float precision), "
"ncs2_vpu: NCS2 VPU, "
"hddl_vpu: HDDL VPU, "
"vulkan: Vulkan, "
"cuda: CUDA, "
"cuda_fp16: CUDA fp16 (half-float preprocess)")
args, _ = parser.parse_known_args()
add_preproc_args(args.zoo, parser, 'mcc', 'mcc')
parser = argparse.ArgumentParser(parents=[parser],
description='''
To run:
Default:
python macbeth_chart_detection.py --input=path/to/your/input/image/or/video (don't give --input flag if want to use device camera)
DNN model:
python macbeth_chart_detection.py mcc --input=path/to/your/input/image/or/video
Model path can also be specified using --model argument. And config path can be specified using --config.
''', formatter_class=argparse.RawTextHelpFormatter)
return parser.parse_args(func_args)
def process_frame(frame, detector, num_charts):
image_copy = frame.copy()
if not detector.process(frame, num_charts):
return None, None
checkers = detector.getListColorChecker()
detector.draw(checkers, frame)
src = checkers[0].getChartsRGB(False)
tgt = detector.getRefColors()
cv.imshow("image result | Press ESC to quit", frame)
cv.imshow("original", image_copy)
return src, tgt
def main(func_args=None):
args = get_args_parser(func_args)
if not (0 <= args.chart_type <= 2):
raise ValueError("chartType must be 0, 1, or 2")
if os.getenv('OPENCV_SAMPLES_DATA_PATH') is not None:
try:
args.model = findModel(args.model, args.sha1)
args.config = findModel(args.config, args.config_sha1)
except:
print("[WARN] Model file not provided, using default detector. Pass model using --model and config using --config to use dnn based detector.\n\n")
args.model = None
args.config = None
else:
args.model = None
args.config = None
print("[WARN] Model file not provided, using default detector. Pass model using --model and config using --config to use dnn based detector. Or, set OPENCV_SAMPLES_DATA_PATH environment variable.\n\n")
if args.model and args.config:
# Load the DNN from TensorFlow model
engine = cv.dnn.ENGINE_AUTO
if args.backend != "default" or args.target != "cpu":
engine = cv.dnn.ENGINE_CLASSIC
net = cv.dnn.readNetFromTensorflow(args.model, args.config, engine)
net.setPreferableBackend(get_backend_id(args.backend))
net.setPreferableTarget(get_target_id(args.target))
detector = cv.mcc_CCheckerDetector.create(net)
print("Detecting checkers using neural network.")
else:
detector = cv.mcc_CCheckerDetector.create()
print("Detecting checkers using default method (no DNN).")
detector.setColorChartType(args.chart_type)
is_video = True
if args.input:
image = cv.imread(findFile(args.input))
if image is not None:
is_video = False
else:
cap = cv.VideoCapture(findFile(args.input))
else:
cap = cv.VideoCapture(0)
if is_video:
print("To print the actual colors and reference colors for current frame press SPACEBAR. To resume press SPACEBAR again")
while True:
ret, frame = cap.read()
if not ret:
break
src, tgt = process_frame(frame, detector, args.num_charts)
key = cv.waitKey(10) & 0xFF
if key == ord(' '):
if src is None or tgt is None:
print("No color chart detected!!")
else:
print("Actual colors: ", src)
print("Reference colors: ", tgt)
print("Press spacebar to resume.")
cv.waitKey(0)
print("Resumed! Processing continues...")
elif key == 27:
exit(0)
if src is not None or tgt is not None:
print("Actual colors: ", src)
print("Reference colors: ", tgt)
else:
src, tgt = process_frame(image, detector, args.num_charts)
if src is None or tgt is None:
print("No color chart detected!!")
else:
print("Actual colors: ", src)
print("Reference colors: ", tgt)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
main()