isbright

PHOTO EMBED

Tue Jun 22 2021 02:54:54 GMT+0000 (Coordinated Universal Time)

Saved by @zhwalker #python #c++

// python
def isbright(image, dim=10, thresh=0.5):
    # Resize image to 10x10
    image = cv2.resize(image, (dim, dim))
    # Convert color space to LAB format and extract L channel
    L, A, B = cv2.split(cv2.cvtColor(image, cv2.COLOR_BGR2LAB))
    # Normalize L channel by dividing all pixel values with maximum pixel value
    L = L/np.max(L)
    # Return True if mean is greater than thresh else False
    return np.mean(L) > thresh

// c++
bool rockface_image_is_bright(cv::Mat img_mat, int dim, float threshold)
{
	// Resize image to 10x10
	cv::resize(img_mat, img_mat, { dim, dim });

	// Convert color space to LAB format and extract L channel
	cv::cvtColor(img_mat, img_mat, cv::COLOR_RGB2Lab);
	cv::Mat labchannel[3];
	cv::split(img_mat, labchannel);

	cv::imshow("L", labchannel[0]);
	cv::waitKey(0);

	// Normalize L channel by dividing all pixel values with maximum pixel value
	cv::Mat L;
	cv::normalize(labchannel[0], L, 0, 1, cv::NORM_MINMAX);

	// Return True if mean is greater than thresh else False
	float brightness = cv::mean(L).val[0];
	std::cout << "brightness: " << brightness << std::endl;
	return brightness > threshold;
}
content_copyCOPY

https://github.com/imneonizer/How-to-find-if-an-image-is-bright-or-dark/