# Correct Colors Using Color Correction Matrix

This example shows how to adjust the colors of an image to better match a standardized set of colors on an Imatest® edge spatial frequency response (eSFR) test chart.

### Obtain Color Correction Matrix from the Test Chart Image

Read an image of a test chart.

`I = imread('eSFRTestImage.jpg'); `

Create an `esfrChart` object that stores information about the test chart. Display the chart, highlighting the 16 color patches. The image has a blue tint.

```chart = esfrChart(I); displayChart(chart,'displayEdgeROIs',false, ... 'displayGrayROIs',false,'displayRegistrationPoints',false)```

Measure the color accuracy of the 16 color patches by using the `measureColor` function. The function also returns the color correction matrix that is used to perform the color correction.

`[colorTable,ccm] = measureColor(chart);`

Compare the measured and reference colors on a color patch diagram. The closer the `Delta_E` value is to 1, the less perceptible the color difference is.

`displayColorPatch(colorTable)`

### Color-Correct the Test Chart Image

Color-correct the original test chart image and display the result.

```I_cc = imapplymatrix(ccm(1:3,:)',I,ccm(4,:)); imshow(I_cc) title('Color-Corrected Image Using Color Patches')```

Create an `esfrChart` object that stores information about the color-corrected test chart. Measure the color accuracy of the 16 color-corrected color patches.

```chart_cc = esfrChart(I_cc); colorTable_cc = measureColor(chart_cc);```

Compare the corrected and reference colors on a color patch diagram. The measured color errors, `delta_E`, are smaller for the color-corrected image than for the original image. Therefore, the colors in this image better agree with the reference colors. However, the chart now has an overall yellow tint and the contrast of the image has decreased.

`displayColorPatch(colorTable_cc)`

### Improve Color Correction Using Gray Patches

You can improve the color correction by including the gray patches as well as the color patches in the least squares fit. Display the original chart, highlighting the 20 gray patches and 16 color patches.

```displayChart(chart,'displayEdgeROIs',false, ... 'displayRegistrationPoints',false)```

Get the reference L*a*b* values of the color and grayscale patches, which are stored in the `ReferenceColorLab` and `ReferenceGrayLab` properties of the eSFR chart object. Convert these values to the RGB color space.

```referenceLab = [chart.ReferenceColorLab; chart.ReferenceGrayLab]; referenceRGB = lab2rgb(referenceLab,'outputtype','uint8');```

Measure the mean gray value in each of the 20 gray patches by using the `measureNoise` function.

```noiseTable = measureNoise(chart); measuredGrayRGB = [noiseTable.MeanIntensity_R, ... noiseTable.MeanIntensity_G, ... noiseTable.MeanIntensity_B];```

Concatenate all measured RGB color values of the color and grayscale patches.

```measuredColorRGB = [colorTable.Measured_R, ... colorTable.Measured_G, ... colorTable.Measured_B]; measuredRGB = [measuredColorRGB; measuredGrayRGB]; ccm_cc = double([measuredRGB ones(36,1)]) \ double(referenceRGB);```

Perform the color correction and display the result. The chart no longer has a yellow tint and the overall appearance of the chart has improved.

```I_cc2 = imapplymatrix(ccm_cc(1:3,:)',I,ccm_cc(4,:)'); imshow(I_cc2) title('Color-Corrected Image Using Gray and Color Patches')```

Compare the corrected and reference colors on a color patch diagram. Some of the measured color errors have decreased, while others have increased.

```chart_cc2 = esfrChart(I_cc2); colorTable_cc2 = measureColor(chart_cc2); displayColorPatch(colorTable_cc2)```