1--C++版本
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cmath>// RGB to HSI
cv::Vec3f RGBtoHSI(cv::Vec3b rgb) {float B = rgb[0] / 255.0f;float G = rgb[1] / 255.0f;float R = rgb[2] / 255.0f;float num = 0.5f * ((R - G) + (R - B));float den = sqrt((R - G)*(R - G) + (R - B)*(G - B));float H = acos(num / (den + 1e-6f)); // 防止除0if (B > G)H = 2 * CV_PI - H;H = H * 180 / CV_PI; // 转为角度float minRGB = std::min({ R, G, B });float sumRGB = R + G + B;float S = 1 - 3 * minRGB / (sumRGB + 1e-6f);float I = sumRGB / 3;return cv::Vec3f(H, S, I);
}// HSI to RGB
cv::Vec3b HSItoRGB(cv::Vec3f hsi) {float H = hsi[0], S = hsi[1], I = hsi[2];float R, G, B;H = H * CV_PI / 180; // 度转弧度if (H < 2 * CV_PI / 3) {B = I * (1 - S);R = I * (1 + (S * cos(H)) / (cos(CV_PI / 3 - H) + 1e-6f));G = 3 * I - (R + B);}else if (H < 4 * CV_PI / 3) {H = H - 2 * CV_PI / 3;R = I * (1 - S);G = I * (1 + (S * cos(H)) / (cos(CV_PI / 3 - H) + 1e-6f));B = 3 * I - (R + G);}else {H = H - 4 * CV_PI / 3;G = I * (1 - S);B = I * (1 + (S * cos(H)) / (cos(CV_PI / 3 - H) + 1e-6f));R = 3 * I - (G + B);}return cv::Vec3b(cv::saturate_cast<uchar>(B * 255),cv::saturate_cast<uchar>(G * 255),cv::saturate_cast<uchar>(R * 255));
}int main() {cv::Mat img = cv::imread("./images_code/Fig0630(01)(strawberries_fullcolor).tif");cv::Mat hsi(img.rows, img.cols, CV_32FC3);// RGB to HSIfor (int i = 0; i < img.rows; i++) {for (int j = 0; j < img.cols; j++) {hsi.at<cv::Vec3f>(i, j) = RGBtoHSI(img.at<cv::Vec3b>(i, j));}}// 修改 HSI 分量for (int i = 0; i < hsi.rows; i++) {for (int j = 0; j < hsi.cols; j++) {cv::Vec3f& pixel = hsi.at<cv::Vec3f>(i, j);// 调整HSI参数:pixel[0] = fmod(pixel[0] + 30, 360); // 色调加30度pixel[1] = std::min(1.0f, pixel[1] * 1.2f); // 饱和度增强20%pixel[2] = std::min(1.0f, pixel[2] * 1.1f); // 亮度增强10%}}// HSI to RGBcv::Mat result(img.rows, img.cols, CV_8UC3);for (int i = 0; i < hsi.rows; i++) {for (int j = 0; j < hsi.cols; j++) {result.at<cv::Vec3b>(i, j) = HSItoRGB(hsi.at<cv::Vec3f>(i, j));}}imwrite("./test.jpg", result);std::cout << "save image to test.jpg" << std::endl;return 0;
}
2--Python版本
import cv2
import numpy as np
import mathdef rgb_to_hsi(pixel):B, G, R = pixel / 255.0num = 0.5 * ((R - G) + (R - B))den = math.sqrt((R - G)**2 + (R - B) * (G - B)) + 1e-6 # 防止除0theta = math.acos(num / den)if B > G:H = 2 * math.pi - thetaelse:H = thetaH = H * 180 / math.pi # 弧度转角度min_val = min(R, G, B)sum_rgb = R + G + BS = 1 - 3 * min_val / (sum_rgb + 1e-6)I = sum_rgb / 3.0return np.array([H, S, I], dtype=np.float32)def hsi_to_rgb(hsi):H, S, I = hsiH_rad = H * math.pi / 180 # 度转弧度if H_rad < 2 * math.pi / 3:B = I * (1 - S)R = I * (1 + S * math.cos(H_rad) / (math.cos(math.pi / 3 - H_rad) + 1e-6))G = 3 * I - (R + B)elif H_rad < 4 * math.pi / 3:H_rad -= 2 * math.pi / 3R = I * (1 - S)G = I * (1 + S * math.cos(H_rad) / (math.cos(math.pi / 3 - H_rad) + 1e-6))B = 3 * I - (R + G)else:H_rad -= 4 * math.pi / 3G = I * (1 - S)B = I * (1 + S * math.cos(H_rad) / (math.cos(math.pi / 3 - H_rad) + 1e-6))R = 3 * I - (G + B)return np.clip([B, G, R], 0, 1) * 255if __name__ == "__main__":# read imageimg = cv2.imread('./images_code/Fig0630(01)(strawberries_fullcolor).tif')rows, cols = img.shape[:2]hsi_img = np.zeros((rows, cols, 3), dtype=np.float32)# RGB → HSIfor i in range(rows):for j in range(cols):hsi_img[i, j] = rgb_to_hsi(img[i, j])# adadjust HSI for i in range(rows):for j in range(cols):H, S, I = hsi_img[i, j]H = (H + 30) % 360S = min(1.0, S * 1.2) # 色调加30度I = min(1.0, I * 1.1) # 饱和度增强20%hsi_img[i, j] = [H, S, I] # 亮度增强10%# HSI → RGBresult = np.zeros_like(img)for i in range(rows):for j in range(cols):rgb = hsi_to_rgb(hsi_img[i, j])result[i, j] = np.uint8(rgb)# save imagecv2.imwrite('./test.jpg', result)print("save image to test.jpg")