2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % CCCC OOO M M PPPP AAA RRRR EEEEE %
7 % C O O MM MM P P A A R R E %
8 % C O O M M M PPPP AAAAA RRRR EEE %
9 % C O O M M P A A R R E %
10 % CCCC OOO M M P A A R R EEEEE %
13 % MagickCore Image Comparison Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/artifact.h"
45 #include "magick/cache-view.h"
46 #include "magick/client.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/colorspace-private.h"
51 #include "magick/compare.h"
52 #include "magick/composite-private.h"
53 #include "magick/constitute.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/image-private.h"
57 #include "magick/list.h"
58 #include "magick/log.h"
59 #include "magick/memory_.h"
60 #include "magick/monitor.h"
61 #include "magick/monitor-private.h"
62 #include "magick/option.h"
63 #include "magick/pixel-private.h"
64 #include "magick/resource_.h"
65 #include "magick/string_.h"
66 #include "magick/statistic.h"
67 #include "magick/transform.h"
68 #include "magick/utility.h"
69 #include "magick/version.h"
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % C o m p a r e I m a g e C h a n n e l s %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % CompareImageChannels() compares one or more image channels of an image
83 % to a reconstructed image and returns the difference image.
85 % The format of the CompareImageChannels method is:
87 % Image *CompareImageChannels(const Image *image,
88 % const Image *reconstruct_image,const ChannelType channel,
89 % const MetricType metric,double *distortion,ExceptionInfo *exception)
91 % A description of each parameter follows:
95 % o reconstruct_image: the reconstruct image.
97 % o channel: the channel.
99 % o metric: the metric.
101 % o distortion: the computed distortion between the images.
103 % o exception: return any errors or warnings in this structure.
107 MagickExport Image *CompareImages(Image *image,const Image *reconstruct_image,
108 const MetricType metric,double *distortion,ExceptionInfo *exception)
113 highlight_image=CompareImageChannels(image,reconstruct_image,AllChannels,
114 metric,distortion,exception);
115 return(highlight_image);
118 MagickExport Image *CompareImageChannels(Image *image,
119 const Image *reconstruct_image,const ChannelType channel,
120 const MetricType metric,double *distortion,ExceptionInfo *exception)
145 assert(image != (Image *) NULL);
146 assert(image->signature == MagickSignature);
147 if (image->debug != MagickFalse)
148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
149 assert(reconstruct_image != (const Image *) NULL);
150 assert(reconstruct_image->signature == MagickSignature);
151 assert(distortion != (double *) NULL);
153 if (image->debug != MagickFalse)
154 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
155 if ((reconstruct_image->columns != image->columns) ||
156 (reconstruct_image->rows != image->rows))
157 ThrowImageException(ImageError,"ImageSizeDiffers");
158 status=GetImageChannelDistortion(image,reconstruct_image,channel,metric,
159 distortion,exception);
160 if (status == MagickFalse)
161 return((Image *) NULL);
162 difference_image=CloneImage(image,0,0,MagickTrue,exception);
163 if (difference_image == (Image *) NULL)
164 return((Image *) NULL);
165 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel);
166 highlight_image=CloneImage(image,image->columns,image->rows,MagickTrue,
168 if (highlight_image == (Image *) NULL)
170 difference_image=DestroyImage(difference_image);
171 return((Image *) NULL);
173 if (SetImageStorageClass(highlight_image,DirectClass) == MagickFalse)
175 InheritException(exception,&highlight_image->exception);
176 difference_image=DestroyImage(difference_image);
177 highlight_image=DestroyImage(highlight_image);
178 return((Image *) NULL);
180 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel);
181 (void) QueryMagickColor("#f1001ecc",&highlight,exception);
182 artifact=GetImageArtifact(image,"highlight-color");
183 if (artifact != (const char *) NULL)
184 (void) QueryMagickColor(artifact,&highlight,exception);
185 (void) QueryMagickColor("#ffffffcc",&lowlight,exception);
186 artifact=GetImageArtifact(image,"lowlight-color");
187 if (artifact != (const char *) NULL)
188 (void) QueryMagickColor(artifact,&lowlight,exception);
189 if (highlight_image->colorspace == CMYKColorspace)
191 ConvertRGBToCMYK(&highlight);
192 ConvertRGBToCMYK(&lowlight);
195 Generate difference image.
198 GetMagickPixelPacket(image,&zero);
199 image_view=AcquireCacheView(image);
200 reconstruct_view=AcquireCacheView(reconstruct_image);
201 highlight_view=AcquireCacheView(highlight_image);
202 #if defined(MAGICKCORE_OPENMP_SUPPORT)
203 #pragma omp parallel for schedule(dynamic,4) shared(status)
205 for (y=0; y < (ssize_t) image->rows; y++)
214 register const IndexPacket
216 *restrict reconstruct_indexes;
218 register const PixelPacket
223 *restrict highlight_indexes;
231 if (status == MagickFalse)
233 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
234 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
236 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,highlight_image->columns,
238 if ((p == (const PixelPacket *) NULL) ||
239 (q == (const PixelPacket *) NULL) || (r == (PixelPacket *) NULL))
244 indexes=GetCacheViewVirtualIndexQueue(image_view);
245 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
246 highlight_indexes=GetCacheViewAuthenticIndexQueue(highlight_view);
248 reconstruct_pixel=zero;
249 for (x=0; x < (ssize_t) image->columns; x++)
254 SetMagickPixelPacket(image,p,indexes+x,&pixel);
255 SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes+x,
257 difference=MagickFalse;
258 if (channel == AllChannels)
260 if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
261 difference=MagickTrue;
265 if (((channel & RedChannel) != 0) && (p->red != q->red))
266 difference=MagickTrue;
267 if (((channel & GreenChannel) != 0) && (p->green != q->green))
268 difference=MagickTrue;
269 if (((channel & BlueChannel) != 0) && (p->blue != q->blue))
270 difference=MagickTrue;
271 if (((channel & OpacityChannel) != 0) &&
272 (image->matte != MagickFalse) && (p->opacity != q->opacity))
273 difference=MagickTrue;
274 if ((((channel & IndexChannel) != 0) &&
275 (image->colorspace == CMYKColorspace) &&
276 (reconstruct_image->colorspace == CMYKColorspace)) &&
277 (indexes[x] != reconstruct_indexes[x]))
278 difference=MagickTrue;
280 if (difference != MagickFalse)
281 SetPixelPacket(highlight_image,&highlight,r,highlight_indexes+x);
283 SetPixelPacket(highlight_image,&lowlight,r,highlight_indexes+x);
288 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
289 if (sync == MagickFalse)
292 highlight_view=DestroyCacheView(highlight_view);
293 reconstruct_view=DestroyCacheView(reconstruct_view);
294 image_view=DestroyCacheView(image_view);
295 (void) CompositeImage(difference_image,image->compose,highlight_image,0,0);
296 highlight_image=DestroyImage(highlight_image);
297 if (status == MagickFalse)
298 difference_image=DestroyImage(difference_image);
299 return(difference_image);
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 % G e t I m a g e C h a n n e l D i s t o r t i o n %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % GetImageChannelDistortion() compares one or more image channels of an image
314 % to a reconstructed image and returns the specified distortion metric.
316 % The format of the CompareImageChannels method is:
318 % MagickBooleanType GetImageChannelDistortion(const Image *image,
319 % const Image *reconstruct_image,const ChannelType channel,
320 % const MetricType metric,double *distortion,ExceptionInfo *exception)
322 % A description of each parameter follows:
324 % o image: the image.
326 % o reconstruct_image: the reconstruct image.
328 % o channel: the channel.
330 % o metric: the metric.
332 % o distortion: the computed distortion between the images.
334 % o exception: return any errors or warnings in this structure.
338 MagickExport MagickBooleanType GetImageDistortion(Image *image,
339 const Image *reconstruct_image,const MetricType metric,double *distortion,
340 ExceptionInfo *exception)
345 status=GetImageChannelDistortion(image,reconstruct_image,AllChannels,
346 metric,distortion,exception);
350 static MagickBooleanType GetAbsoluteDistortion(const Image *image,
351 const Image *reconstruct_image,const ChannelType channel,double *distortion,
352 ExceptionInfo *exception)
368 Compute the absolute difference in pixels between two images.
371 GetMagickPixelPacket(image,&zero);
372 image_view=AcquireCacheView(image);
373 reconstruct_view=AcquireCacheView(reconstruct_image);
374 #if defined(MAGICKCORE_OPENMP_SUPPORT)
375 #pragma omp parallel for schedule(dynamic,4) shared(status)
377 for (y=0; y < (ssize_t) image->rows; y++)
380 channel_distortion[AllChannels+1];
386 register const IndexPacket
388 *restrict reconstruct_indexes;
390 register const PixelPacket
398 if (status == MagickFalse)
400 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
401 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
403 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
408 indexes=GetCacheViewVirtualIndexQueue(image_view);
409 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
411 reconstruct_pixel=pixel;
412 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
413 for (x=0; x < (ssize_t) image->columns; x++)
415 SetMagickPixelPacket(image,p,indexes+x,&pixel);
416 SetMagickPixelPacket(reconstruct_image,q,reconstruct_indexes+x,
418 if (IsMagickColorSimilar(&pixel,&reconstruct_pixel) == MagickFalse)
420 if ((channel & RedChannel) != 0)
421 channel_distortion[RedChannel]++;
422 if ((channel & GreenChannel) != 0)
423 channel_distortion[GreenChannel]++;
424 if ((channel & BlueChannel) != 0)
425 channel_distortion[BlueChannel]++;
426 if (((channel & OpacityChannel) != 0) &&
427 (image->matte != MagickFalse))
428 channel_distortion[OpacityChannel]++;
429 if (((channel & IndexChannel) != 0) &&
430 (image->colorspace == CMYKColorspace))
431 channel_distortion[BlackChannel]++;
432 channel_distortion[AllChannels]++;
437 #if defined(MAGICKCORE_OPENMP_SUPPORT)
438 #pragma omp critical (MagickCore_GetAbsoluteError)
440 for (i=0; i <= (ssize_t) AllChannels; i++)
441 distortion[i]+=channel_distortion[i];
443 reconstruct_view=DestroyCacheView(reconstruct_view);
444 image_view=DestroyCacheView(image_view);
448 static size_t GetNumberChannels(const Image *image,
449 const ChannelType channel)
455 if ((channel & RedChannel) != 0)
457 if ((channel & GreenChannel) != 0)
459 if ((channel & BlueChannel) != 0)
461 if (((channel & OpacityChannel) != 0) &&
462 (image->matte != MagickFalse))
464 if (((channel & IndexChannel) != 0) &&
465 (image->colorspace == CMYKColorspace))
470 static MagickBooleanType GetMeanAbsoluteDistortion(const Image *image,
471 const Image *reconstruct_image,const ChannelType channel,
472 double *distortion,ExceptionInfo *exception)
488 image_view=AcquireCacheView(image);
489 reconstruct_view=AcquireCacheView(reconstruct_image);
490 #if defined(MAGICKCORE_OPENMP_SUPPORT)
491 #pragma omp parallel for schedule(dynamic,4) shared(status)
493 for (y=0; y < (ssize_t) image->rows; y++)
496 channel_distortion[AllChannels+1];
498 register const IndexPacket
500 *restrict reconstruct_indexes;
502 register const PixelPacket
510 if (status == MagickFalse)
512 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
513 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
514 reconstruct_image->columns,1,exception);
515 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
520 indexes=GetCacheViewVirtualIndexQueue(image_view);
521 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
522 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
523 for (x=0; x < (ssize_t) image->columns; x++)
528 if ((channel & RedChannel) != 0)
530 distance=QuantumScale*fabs(p->red-(double) q->red);
531 channel_distortion[RedChannel]+=distance;
532 channel_distortion[AllChannels]+=distance;
534 if ((channel & GreenChannel) != 0)
536 distance=QuantumScale*fabs(p->green-(double) q->green);
537 channel_distortion[GreenChannel]+=distance;
538 channel_distortion[AllChannels]+=distance;
540 if ((channel & BlueChannel) != 0)
542 distance=QuantumScale*fabs(p->blue-(double) q->blue);
543 channel_distortion[BlueChannel]+=distance;
544 channel_distortion[AllChannels]+=distance;
546 if (((channel & OpacityChannel) != 0) &&
547 (image->matte != MagickFalse))
549 distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
550 channel_distortion[OpacityChannel]+=distance;
551 channel_distortion[AllChannels]+=distance;
553 if (((channel & IndexChannel) != 0) &&
554 (image->colorspace == CMYKColorspace))
556 distance=QuantumScale*fabs(indexes[x]-(double)
557 reconstruct_indexes[x]);
558 channel_distortion[BlackChannel]+=distance;
559 channel_distortion[AllChannels]+=distance;
564 #if defined(MAGICKCORE_OPENMP_SUPPORT)
565 #pragma omp critical (MagickCore_GetMeanAbsoluteError)
567 for (i=0; i <= (ssize_t) AllChannels; i++)
568 distortion[i]+=channel_distortion[i];
570 reconstruct_view=DestroyCacheView(reconstruct_view);
571 image_view=DestroyCacheView(image_view);
572 for (i=0; i <= (ssize_t) AllChannels; i++)
573 distortion[i]/=((double) image->columns*image->rows);
574 distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
578 static MagickBooleanType GetMeanErrorPerPixel(Image *image,
579 const Image *reconstruct_image,const ChannelType channel,double *distortion,
580 ExceptionInfo *exception)
605 image_view=AcquireCacheView(image);
606 reconstruct_view=AcquireCacheView(reconstruct_image);
607 for (y=0; y < (ssize_t) image->rows; y++)
609 register const IndexPacket
611 *restrict reconstruct_indexes;
613 register const PixelPacket
620 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
621 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
623 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
628 indexes=GetCacheViewVirtualIndexQueue(image_view);
629 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
630 for (x=0; x < (ssize_t) image->columns; x++)
635 if ((channel & OpacityChannel) != 0)
637 if (image->matte != MagickFalse)
638 alpha=(MagickRealType) (QuantumScale*(GetAlphaPixelComponent(p)));
639 if (reconstruct_image->matte != MagickFalse)
640 beta=(MagickRealType) (QuantumScale*GetAlphaPixelComponent(q));
642 if ((channel & RedChannel) != 0)
644 distance=fabs(alpha*p->red-beta*q->red);
645 distortion[RedChannel]+=distance;
646 distortion[AllChannels]+=distance;
647 mean_error+=distance*distance;
648 if (distance > maximum_error)
649 maximum_error=distance;
652 if ((channel & GreenChannel) != 0)
654 distance=fabs(alpha*p->green-beta*q->green);
655 distortion[GreenChannel]+=distance;
656 distortion[AllChannels]+=distance;
657 mean_error+=distance*distance;
658 if (distance > maximum_error)
659 maximum_error=distance;
662 if ((channel & BlueChannel) != 0)
664 distance=fabs(alpha*p->blue-beta*q->blue);
665 distortion[BlueChannel]+=distance;
666 distortion[AllChannels]+=distance;
667 mean_error+=distance*distance;
668 if (distance > maximum_error)
669 maximum_error=distance;
672 if (((channel & OpacityChannel) != 0) &&
673 (image->matte != MagickFalse))
675 distance=fabs((double) p->opacity-q->opacity);
676 distortion[OpacityChannel]+=distance;
677 distortion[AllChannels]+=distance;
678 mean_error+=distance*distance;
679 if (distance > maximum_error)
680 maximum_error=distance;
683 if (((channel & IndexChannel) != 0) &&
684 (image->colorspace == CMYKColorspace) &&
685 (reconstruct_image->colorspace == CMYKColorspace))
687 distance=fabs(alpha*indexes[x]-beta*reconstruct_indexes[x]);
688 distortion[BlackChannel]+=distance;
689 distortion[AllChannels]+=distance;
690 mean_error+=distance*distance;
691 if (distance > maximum_error)
692 maximum_error=distance;
699 reconstruct_view=DestroyCacheView(reconstruct_view);
700 image_view=DestroyCacheView(image_view);
701 image->error.mean_error_per_pixel=distortion[AllChannels]/area;
702 image->error.normalized_mean_error=QuantumScale*QuantumScale*mean_error/area;
703 image->error.normalized_maximum_error=QuantumScale*maximum_error;
707 static MagickBooleanType GetMeanSquaredDistortion(const Image *image,
708 const Image *reconstruct_image,const ChannelType channel,
709 double *distortion,ExceptionInfo *exception)
725 image_view=AcquireCacheView(image);
726 reconstruct_view=AcquireCacheView(reconstruct_image);
727 #if defined(MAGICKCORE_OPENMP_SUPPORT)
728 #pragma omp parallel for schedule(dynamic,4) shared(status)
730 for (y=0; y < (ssize_t) image->rows; y++)
733 channel_distortion[AllChannels+1];
735 register const IndexPacket
737 *restrict reconstruct_indexes;
739 register const PixelPacket
747 if (status == MagickFalse)
749 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
750 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
751 reconstruct_image->columns,1,exception);
752 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
757 indexes=GetCacheViewVirtualIndexQueue(image_view);
758 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
759 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
760 for (x=0; x < (ssize_t) image->columns; x++)
765 if ((channel & RedChannel) != 0)
767 distance=QuantumScale*(p->red-(MagickRealType) q->red);
768 channel_distortion[RedChannel]+=distance*distance;
769 channel_distortion[AllChannels]+=distance*distance;
771 if ((channel & GreenChannel) != 0)
773 distance=QuantumScale*(p->green-(MagickRealType) q->green);
774 channel_distortion[GreenChannel]+=distance*distance;
775 channel_distortion[AllChannels]+=distance*distance;
777 if ((channel & BlueChannel) != 0)
779 distance=QuantumScale*(p->blue-(MagickRealType) q->blue);
780 channel_distortion[BlueChannel]+=distance*distance;
781 channel_distortion[AllChannels]+=distance*distance;
783 if (((channel & OpacityChannel) != 0) &&
784 (image->matte != MagickFalse))
786 distance=QuantumScale*(p->opacity-(MagickRealType) q->opacity);
787 channel_distortion[OpacityChannel]+=distance*distance;
788 channel_distortion[AllChannels]+=distance*distance;
790 if (((channel & IndexChannel) != 0) &&
791 (image->colorspace == CMYKColorspace) &&
792 (reconstruct_image->colorspace == CMYKColorspace))
794 distance=QuantumScale*(indexes[x]-(MagickRealType)
795 reconstruct_indexes[x]);
796 channel_distortion[BlackChannel]+=distance*distance;
797 channel_distortion[AllChannels]+=distance*distance;
802 #if defined(MAGICKCORE_OPENMP_SUPPORT)
803 #pragma omp critical (MagickCore_GetMeanSquaredError)
805 for (i=0; i <= (ssize_t) AllChannels; i++)
806 distortion[i]+=channel_distortion[i];
808 reconstruct_view=DestroyCacheView(reconstruct_view);
809 image_view=DestroyCacheView(image_view);
810 for (i=0; i <= (ssize_t) AllChannels; i++)
811 distortion[i]/=((double) image->columns*image->rows);
812 distortion[AllChannels]/=(double) GetNumberChannels(image,channel);
816 static MagickBooleanType GetNormalizedCrossCorrelationDistortion(
817 const Image *image,const Image *reconstruct_image,const ChannelType channel,
818 double *distortion,ExceptionInfo *exception)
820 #define SimilarityImageTag "Similarity/Image"
828 *reconstruct_statistics;
846 Normalize to account for variation due to lighting and exposure condition.
848 image_statistics=GetImageChannelStatistics(image,exception);
849 reconstruct_statistics=GetImageChannelStatistics(reconstruct_image,exception);
852 for (i=0; i <= (ssize_t) AllChannels; i++)
854 area=1.0/((MagickRealType) image->columns*image->rows);
855 image_view=AcquireCacheView(image);
856 reconstruct_view=AcquireCacheView(reconstruct_image);
857 for (y=0; y < (ssize_t) image->rows; y++)
859 register const IndexPacket
861 *restrict reconstruct_indexes;
863 register const PixelPacket
870 if (status == MagickFalse)
872 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
873 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
875 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
880 indexes=GetCacheViewVirtualIndexQueue(image_view);
881 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
882 for (x=0; x < (ssize_t) image->columns; x++)
884 if ((channel & RedChannel) != 0)
885 distortion[RedChannel]+=area*QuantumScale*(p->red-
886 image_statistics[RedChannel].mean)*(q->red-
887 reconstruct_statistics[RedChannel].mean);
888 if ((channel & GreenChannel) != 0)
889 distortion[GreenChannel]+=area*QuantumScale*(p->green-
890 image_statistics[GreenChannel].mean)*(q->green-
891 reconstruct_statistics[GreenChannel].mean);
892 if ((channel & BlueChannel) != 0)
893 distortion[BlueChannel]+=area*QuantumScale*(p->blue-
894 image_statistics[BlueChannel].mean)*(q->blue-
895 reconstruct_statistics[BlueChannel].mean);
896 if (((channel & OpacityChannel) != 0) &&
897 (image->matte != MagickFalse))
898 distortion[OpacityChannel]+=area*QuantumScale*(p->opacity-
899 image_statistics[OpacityChannel].mean)*(q->opacity-
900 reconstruct_statistics[OpacityChannel].mean);
901 if (((channel & IndexChannel) != 0) &&
902 (image->colorspace == CMYKColorspace) &&
903 (reconstruct_image->colorspace == CMYKColorspace))
904 distortion[BlackChannel]+=area*QuantumScale*(indexes[x]-
905 image_statistics[OpacityChannel].mean)*(reconstruct_indexes[x]-
906 reconstruct_statistics[OpacityChannel].mean);
910 if (image->progress_monitor != (MagickProgressMonitor) NULL)
915 proceed=SetImageProgress(image,SimilarityImageTag,progress++,
917 if (proceed == MagickFalse)
921 reconstruct_view=DestroyCacheView(reconstruct_view);
922 image_view=DestroyCacheView(image_view);
924 Divide by the standard deviation.
926 for (i=0; i < (ssize_t) AllChannels; i++)
931 gamma=image_statistics[i].standard_deviation*
932 reconstruct_statistics[i].standard_deviation;
933 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
934 distortion[i]=QuantumRange*gamma*distortion[i];
936 distortion[AllChannels]=0.0;
937 if ((channel & RedChannel) != 0)
938 distortion[AllChannels]+=distortion[RedChannel]*distortion[RedChannel];
939 if ((channel & GreenChannel) != 0)
940 distortion[AllChannels]+=distortion[GreenChannel]*distortion[GreenChannel];
941 if ((channel & BlueChannel) != 0)
942 distortion[AllChannels]+=distortion[BlueChannel]*distortion[BlueChannel];
943 if (((channel & OpacityChannel) != 0) && (image->matte != MagickFalse))
944 distortion[AllChannels]+=distortion[OpacityChannel]*
945 distortion[OpacityChannel];
946 if (((channel & IndexChannel) != 0) &&
947 (image->colorspace == CMYKColorspace))
948 distortion[AllChannels]+=distortion[BlackChannel]*distortion[BlackChannel];
949 distortion[AllChannels]=sqrt(distortion[AllChannels]/GetNumberChannels(image,
954 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
955 reconstruct_statistics);
956 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
961 static MagickBooleanType GetPeakAbsoluteDistortion(const Image *image,
962 const Image *reconstruct_image,const ChannelType channel,
963 double *distortion,ExceptionInfo *exception)
976 image_view=AcquireCacheView(image);
977 reconstruct_view=AcquireCacheView(reconstruct_image);
978 #if defined(MAGICKCORE_OPENMP_SUPPORT)
979 #pragma omp parallel for schedule(dynamic,4) shared(status)
981 for (y=0; y < (ssize_t) image->rows; y++)
984 channel_distortion[AllChannels+1];
986 register const IndexPacket
988 *restrict reconstruct_indexes;
990 register const PixelPacket
998 if (status == MagickFalse)
1000 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1001 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,
1002 reconstruct_image->columns,1,exception);
1003 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
1008 indexes=GetCacheViewVirtualIndexQueue(image_view);
1009 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1010 (void) ResetMagickMemory(channel_distortion,0,sizeof(channel_distortion));
1011 for (x=0; x < (ssize_t) image->columns; x++)
1016 if ((channel & RedChannel) != 0)
1018 distance=QuantumScale*fabs(p->red-(double) q->red);
1019 if (distance > channel_distortion[RedChannel])
1020 channel_distortion[RedChannel]=distance;
1021 if (distance > channel_distortion[AllChannels])
1022 channel_distortion[AllChannels]=distance;
1024 if ((channel & GreenChannel) != 0)
1026 distance=QuantumScale*fabs(p->green-(double) q->green);
1027 if (distance > channel_distortion[GreenChannel])
1028 channel_distortion[GreenChannel]=distance;
1029 if (distance > channel_distortion[AllChannels])
1030 channel_distortion[AllChannels]=distance;
1032 if ((channel & BlueChannel) != 0)
1034 distance=QuantumScale*fabs(p->blue-(double) q->blue);
1035 if (distance > channel_distortion[BlueChannel])
1036 channel_distortion[BlueChannel]=distance;
1037 if (distance > channel_distortion[AllChannels])
1038 channel_distortion[AllChannels]=distance;
1040 if (((channel & OpacityChannel) != 0) &&
1041 (image->matte != MagickFalse))
1043 distance=QuantumScale*fabs(p->opacity-(double) q->opacity);
1044 if (distance > channel_distortion[OpacityChannel])
1045 channel_distortion[OpacityChannel]=distance;
1046 if (distance > channel_distortion[AllChannels])
1047 channel_distortion[AllChannels]=distance;
1049 if (((channel & IndexChannel) != 0) &&
1050 (image->colorspace == CMYKColorspace) &&
1051 (reconstruct_image->colorspace == CMYKColorspace))
1053 distance=QuantumScale*fabs(indexes[x]-(double)
1054 reconstruct_indexes[x]);
1055 if (distance > channel_distortion[BlackChannel])
1056 channel_distortion[BlackChannel]=distance;
1057 if (distance > channel_distortion[AllChannels])
1058 channel_distortion[AllChannels]=distance;
1063 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1064 #pragma omp critical (MagickCore_GetPeakAbsoluteError)
1066 for (i=0; i <= (ssize_t) AllChannels; i++)
1067 if (channel_distortion[i] > distortion[i])
1068 distortion[i]=channel_distortion[i];
1070 reconstruct_view=DestroyCacheView(reconstruct_view);
1071 image_view=DestroyCacheView(image_view);
1075 static MagickBooleanType GetPeakSignalToNoiseRatio(const Image *image,
1076 const Image *reconstruct_image,const ChannelType channel,
1077 double *distortion,ExceptionInfo *exception)
1082 status=GetMeanSquaredDistortion(image,reconstruct_image,channel,distortion,
1084 if ((channel & RedChannel) != 0)
1085 distortion[RedChannel]=20.0*log10((double) 1.0/sqrt(
1086 distortion[RedChannel]));
1087 if ((channel & GreenChannel) != 0)
1088 distortion[GreenChannel]=20.0*log10((double) 1.0/sqrt(
1089 distortion[GreenChannel]));
1090 if ((channel & BlueChannel) != 0)
1091 distortion[BlueChannel]=20.0*log10((double) 1.0/sqrt(
1092 distortion[BlueChannel]));
1093 if (((channel & OpacityChannel) != 0) &&
1094 (image->matte != MagickFalse))
1095 distortion[OpacityChannel]=20.0*log10((double) 1.0/sqrt(
1096 distortion[OpacityChannel]));
1097 if (((channel & IndexChannel) != 0) &&
1098 (image->colorspace == CMYKColorspace))
1099 distortion[BlackChannel]=20.0*log10((double) 1.0/sqrt(
1100 distortion[BlackChannel]));
1101 distortion[AllChannels]=20.0*log10((double) 1.0/sqrt(
1102 distortion[AllChannels]));
1106 static MagickBooleanType GetRootMeanSquaredDistortion(const Image *image,
1107 const Image *reconstruct_image,const ChannelType channel,
1108 double *distortion,ExceptionInfo *exception)
1113 status=GetMeanSquaredDistortion(image,reconstruct_image,channel,distortion,
1115 if ((channel & RedChannel) != 0)
1116 distortion[RedChannel]=sqrt(distortion[RedChannel]);
1117 if ((channel & GreenChannel) != 0)
1118 distortion[GreenChannel]=sqrt(distortion[GreenChannel]);
1119 if ((channel & BlueChannel) != 0)
1120 distortion[BlueChannel]=sqrt(distortion[BlueChannel]);
1121 if (((channel & OpacityChannel) != 0) &&
1122 (image->matte != MagickFalse))
1123 distortion[OpacityChannel]=sqrt(distortion[OpacityChannel]);
1124 if (((channel & IndexChannel) != 0) &&
1125 (image->colorspace == CMYKColorspace))
1126 distortion[BlackChannel]=sqrt(distortion[BlackChannel]);
1127 distortion[AllChannels]=sqrt(distortion[AllChannels]);
1131 MagickExport MagickBooleanType GetImageChannelDistortion(Image *image,
1132 const Image *reconstruct_image,const ChannelType channel,
1133 const MetricType metric,double *distortion,ExceptionInfo *exception)
1136 *channel_distortion;
1144 assert(image != (Image *) NULL);
1145 assert(image->signature == MagickSignature);
1146 if (image->debug != MagickFalse)
1147 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1148 assert(reconstruct_image != (const Image *) NULL);
1149 assert(reconstruct_image->signature == MagickSignature);
1150 assert(distortion != (double *) NULL);
1152 if (image->debug != MagickFalse)
1153 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1154 if ((reconstruct_image->columns != image->columns) ||
1155 (reconstruct_image->rows != image->rows))
1156 ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
1158 Get image distortion.
1160 length=AllChannels+1UL;
1161 channel_distortion=(double *) AcquireQuantumMemory(length,
1162 sizeof(*channel_distortion));
1163 if (channel_distortion == (double *) NULL)
1164 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1165 (void) ResetMagickMemory(channel_distortion,0,length*
1166 sizeof(*channel_distortion));
1169 case AbsoluteErrorMetric:
1171 status=GetAbsoluteDistortion(image,reconstruct_image,channel,
1172 channel_distortion,exception);
1175 case MeanAbsoluteErrorMetric:
1177 status=GetMeanAbsoluteDistortion(image,reconstruct_image,channel,
1178 channel_distortion,exception);
1181 case MeanErrorPerPixelMetric:
1183 status=GetMeanErrorPerPixel(image,reconstruct_image,channel,
1184 channel_distortion,exception);
1187 case MeanSquaredErrorMetric:
1189 status=GetMeanSquaredDistortion(image,reconstruct_image,channel,
1190 channel_distortion,exception);
1193 case NormalizedCrossCorrelationErrorMetric:
1196 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1197 channel,channel_distortion,exception);
1200 case PeakAbsoluteErrorMetric:
1202 status=GetPeakAbsoluteDistortion(image,reconstruct_image,channel,
1203 channel_distortion,exception);
1206 case PeakSignalToNoiseRatioMetric:
1208 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,channel,
1209 channel_distortion,exception);
1212 case RootMeanSquaredErrorMetric:
1214 status=GetRootMeanSquaredDistortion(image,reconstruct_image,channel,
1215 channel_distortion,exception);
1219 *distortion=channel_distortion[AllChannels];
1220 channel_distortion=(double *) RelinquishMagickMemory(channel_distortion);
1225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1229 % G e t I m a g e C h a n n e l D i s t o r t i o n s %
1233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1235 % GetImageChannelDistrortion() compares the image channels of an image to a
1236 % reconstructed image and returns the specified distortion metric for each
1239 % The format of the CompareImageChannels method is:
1241 % double *GetImageChannelDistortions(const Image *image,
1242 % const Image *reconstruct_image,const MetricType metric,
1243 % ExceptionInfo *exception)
1245 % A description of each parameter follows:
1247 % o image: the image.
1249 % o reconstruct_image: the reconstruct image.
1251 % o metric: the metric.
1253 % o exception: return any errors or warnings in this structure.
1256 MagickExport double *GetImageChannelDistortions(Image *image,
1257 const Image *reconstruct_image,const MetricType metric,
1258 ExceptionInfo *exception)
1261 *channel_distortion;
1269 assert(image != (Image *) NULL);
1270 assert(image->signature == MagickSignature);
1271 if (image->debug != MagickFalse)
1272 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1273 assert(reconstruct_image != (const Image *) NULL);
1274 assert(reconstruct_image->signature == MagickSignature);
1275 if (image->debug != MagickFalse)
1276 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1277 if ((reconstruct_image->columns != image->columns) ||
1278 (reconstruct_image->rows != image->rows))
1280 (void) ThrowMagickException(&image->exception,GetMagickModule(),
1281 ImageError,"ImageSizeDiffers","`%s'",image->filename);
1282 return((double *) NULL);
1285 Get image distortion.
1287 length=AllChannels+1UL;
1288 channel_distortion=(double *) AcquireQuantumMemory(length,
1289 sizeof(*channel_distortion));
1290 if (channel_distortion == (double *) NULL)
1291 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1292 (void) ResetMagickMemory(channel_distortion,0,length*
1293 sizeof(*channel_distortion));
1296 case AbsoluteErrorMetric:
1298 status=GetAbsoluteDistortion(image,reconstruct_image,AllChannels,
1299 channel_distortion,exception);
1302 case MeanAbsoluteErrorMetric:
1304 status=GetMeanAbsoluteDistortion(image,reconstruct_image,AllChannels,
1305 channel_distortion,exception);
1308 case MeanErrorPerPixelMetric:
1310 status=GetMeanErrorPerPixel(image,reconstruct_image,AllChannels,
1311 channel_distortion,exception);
1314 case MeanSquaredErrorMetric:
1316 status=GetMeanSquaredDistortion(image,reconstruct_image,AllChannels,
1317 channel_distortion,exception);
1320 case NormalizedCrossCorrelationErrorMetric:
1323 status=GetNormalizedCrossCorrelationDistortion(image,reconstruct_image,
1324 AllChannels,channel_distortion,exception);
1327 case PeakAbsoluteErrorMetric:
1329 status=GetPeakAbsoluteDistortion(image,reconstruct_image,AllChannels,
1330 channel_distortion,exception);
1333 case PeakSignalToNoiseRatioMetric:
1335 status=GetPeakSignalToNoiseRatio(image,reconstruct_image,AllChannels,
1336 channel_distortion,exception);
1339 case RootMeanSquaredErrorMetric:
1341 status=GetRootMeanSquaredDistortion(image,reconstruct_image,AllChannels,
1342 channel_distortion,exception);
1346 return(channel_distortion);
1350 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354 % I s I m a g e s E q u a l %
1358 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360 % IsImagesEqual() measures the difference between colors at each pixel
1361 % location of two images. A value other than 0 means the colors match
1362 % exactly. Otherwise an error measure is computed by summing over all
1363 % pixels in an image the distance squared in RGB space between each image
1364 % pixel and its corresponding pixel in the reconstruct image. The error
1365 % measure is assigned to these image members:
1367 % o mean_error_per_pixel: The mean error for any single pixel in
1370 % o normalized_mean_error: The normalized mean quantization error for
1371 % any single pixel in the image. This distance measure is normalized to
1372 % a range between 0 and 1. It is independent of the range of red, green,
1373 % and blue values in the image.
1375 % o normalized_maximum_error: The normalized maximum quantization
1376 % error for any single pixel in the image. This distance measure is
1377 % normalized to a range between 0 and 1. It is independent of the range
1378 % of red, green, and blue values in your image.
1380 % A small normalized mean square error, accessed as
1381 % image->normalized_mean_error, suggests the images are very similar in
1382 % spatial layout and color.
1384 % The format of the IsImagesEqual method is:
1386 % MagickBooleanType IsImagesEqual(Image *image,
1387 % const Image *reconstruct_image)
1389 % A description of each parameter follows.
1391 % o image: the image.
1393 % o reconstruct_image: the reconstruct image.
1396 MagickExport MagickBooleanType IsImagesEqual(Image *image,
1397 const Image *reconstruct_image)
1416 mean_error_per_pixel;
1418 assert(image != (Image *) NULL);
1419 assert(image->signature == MagickSignature);
1420 assert(reconstruct_image != (const Image *) NULL);
1421 assert(reconstruct_image->signature == MagickSignature);
1422 if ((reconstruct_image->columns != image->columns) ||
1423 (reconstruct_image->rows != image->rows))
1424 ThrowBinaryException(ImageError,"ImageSizeDiffers",image->filename);
1427 mean_error_per_pixel=0.0;
1429 exception=(&image->exception);
1430 image_view=AcquireCacheView(image);
1431 reconstruct_view=AcquireCacheView(reconstruct_image);
1432 for (y=0; y < (ssize_t) image->rows; y++)
1434 register const IndexPacket
1436 *restrict reconstruct_indexes;
1438 register const PixelPacket
1445 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1446 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
1448 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
1450 indexes=GetCacheViewVirtualIndexQueue(image_view);
1451 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1452 for (x=0; x < (ssize_t) image->columns; x++)
1457 distance=fabs(p->red-(double) q->red);
1458 mean_error_per_pixel+=distance;
1459 mean_error+=distance*distance;
1460 if (distance > maximum_error)
1461 maximum_error=distance;
1463 distance=fabs(p->green-(double) q->green);
1464 mean_error_per_pixel+=distance;
1465 mean_error+=distance*distance;
1466 if (distance > maximum_error)
1467 maximum_error=distance;
1469 distance=fabs(p->blue-(double) q->blue);
1470 mean_error_per_pixel+=distance;
1471 mean_error+=distance*distance;
1472 if (distance > maximum_error)
1473 maximum_error=distance;
1475 if (image->matte != MagickFalse)
1477 distance=fabs(p->opacity-(double) q->opacity);
1478 mean_error_per_pixel+=distance;
1479 mean_error+=distance*distance;
1480 if (distance > maximum_error)
1481 maximum_error=distance;
1484 if ((image->colorspace == CMYKColorspace) &&
1485 (reconstruct_image->colorspace == CMYKColorspace))
1487 distance=fabs(indexes[x]-(double) reconstruct_indexes[x]);
1488 mean_error_per_pixel+=distance;
1489 mean_error+=distance*distance;
1490 if (distance > maximum_error)
1491 maximum_error=distance;
1498 reconstruct_view=DestroyCacheView(reconstruct_view);
1499 image_view=DestroyCacheView(image_view);
1500 image->error.mean_error_per_pixel=(double) (mean_error_per_pixel/area);
1501 image->error.normalized_mean_error=(double) (QuantumScale*QuantumScale*
1503 image->error.normalized_maximum_error=(double) (QuantumScale*maximum_error);
1504 status=image->error.mean_error_per_pixel == 0.0 ? MagickTrue : MagickFalse;
1509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1513 % S i m i l a r i t y I m a g e %
1517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1519 % SimilarityImage() compares the reference image of the image and returns the
1520 % best match offset. In addition, it returns a similarity image such that an
1521 % exact match location is completely white and if none of the pixels match,
1522 % black, otherwise some gray level in-between.
1524 % The format of the SimilarityImageImage method is:
1526 % Image *SimilarityImage(const Image *image,const Image *reference,
1527 % RectangleInfo *offset,double *similarity,ExceptionInfo *exception)
1529 % A description of each parameter follows:
1531 % o image: the image.
1533 % o reference: find an area of the image that closely resembles this image.
1535 % o the best match offset of the reference image within the image.
1537 % o similarity: the computed similarity between the images.
1539 % o exception: return any errors or warnings in this structure.
1543 static double GetNCCDistortion(const Image *image,
1544 const Image *reconstruct_image,
1545 const ChannelStatistics *reconstruct_statistics,ExceptionInfo *exception)
1547 #define SimilarityImageTag "Similarity/Image"
1573 Normalize to account for variation due to lighting and exposure condition.
1575 image_statistics=GetImageChannelStatistics(image,exception);
1578 area=1.0/((MagickRealType) image->columns*image->rows);
1579 image_view=AcquireCacheView(image);
1580 reconstruct_view=AcquireCacheView(reconstruct_image);
1581 for (y=0; y < (ssize_t) image->rows; y++)
1583 register const IndexPacket
1585 *restrict reconstruct_indexes;
1587 register const PixelPacket
1594 if (status == MagickFalse)
1596 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1597 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,reconstruct_image->columns,
1599 if ((p == (const PixelPacket *) NULL) || (q == (const PixelPacket *) NULL))
1604 indexes=GetCacheViewVirtualIndexQueue(image_view);
1605 reconstruct_indexes=GetCacheViewVirtualIndexQueue(reconstruct_view);
1606 for (x=0; x < (ssize_t) image->columns; x++)
1608 distortion+=area*QuantumScale*(p->red-
1609 image_statistics[RedChannel].mean)*(q->red-
1610 reconstruct_statistics[RedChannel].mean);
1611 distortion+=area*QuantumScale*(p->green-
1612 image_statistics[GreenChannel].mean)*(q->green-
1613 reconstruct_statistics[GreenChannel].mean);
1614 distortion+=area*QuantumScale*(p->blue-
1615 image_statistics[BlueChannel].mean)*(q->blue-
1616 reconstruct_statistics[BlueChannel].mean);
1617 if (image->matte != MagickFalse)
1618 distortion+=area*QuantumScale*(p->opacity-
1619 image_statistics[OpacityChannel].mean)*(q->opacity-
1620 reconstruct_statistics[OpacityChannel].mean);
1621 if ((image->colorspace == CMYKColorspace) &&
1622 (reconstruct_image->colorspace == CMYKColorspace))
1623 distortion+=area*QuantumScale*(indexes[x]-
1624 image_statistics[OpacityChannel].mean)*(reconstruct_indexes[x]-
1625 reconstruct_statistics[OpacityChannel].mean);
1630 reconstruct_view=DestroyCacheView(reconstruct_view);
1631 image_view=DestroyCacheView(image_view);
1633 Divide by the standard deviation.
1635 gamma=image_statistics[AllChannels].standard_deviation*
1636 reconstruct_statistics[AllChannels].standard_deviation;
1637 gamma=1.0/(fabs((double) gamma) <= MagickEpsilon ? 1.0 : gamma);
1638 distortion=QuantumRange*gamma*distortion;
1640 if (image->matte != MagickFalse)
1642 if (image->colorspace == CMYKColorspace)
1644 distortion=sqrt(distortion/number_channels);
1648 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1650 return(1.0-distortion);
1653 static double GetSimilarityMetric(const Image *image,const Image *reference,
1654 const ChannelStatistics *reference_statistics,const ssize_t x_offset,
1655 const ssize_t y_offset,ExceptionInfo *exception)
1666 SetGeometry(reference,&geometry);
1667 geometry.x=x_offset;
1668 geometry.y=y_offset;
1669 similarity_image=CropImage(image,&geometry,exception);
1670 if (similarity_image == (Image *) NULL)
1672 distortion=GetNCCDistortion(reference,similarity_image,reference_statistics,
1674 similarity_image=DestroyImage(similarity_image);
1678 MagickExport Image *SimilarityImage(Image *image,const Image *reference,
1679 RectangleInfo *offset,double *similarity_metric,ExceptionInfo *exception)
1681 #define SimilarityImageTag "Similarity/Image"
1687 *reference_statistics;
1701 assert(image != (const Image *) NULL);
1702 assert(image->signature == MagickSignature);
1703 if (image->debug != MagickFalse)
1704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1705 assert(exception != (ExceptionInfo *) NULL);
1706 assert(exception->signature == MagickSignature);
1707 assert(offset != (RectangleInfo *) NULL);
1708 SetGeometry(reference,offset);
1709 *similarity_metric=1.0;
1710 if ((reference->columns > image->columns) || (reference->rows > image->rows))
1711 ThrowImageException(ImageError,"ImageSizeDiffers");
1712 similarity_image=CloneImage(image,image->columns-reference->columns+1,
1713 image->rows-reference->rows+1,MagickTrue,exception);
1714 if (similarity_image == (Image *) NULL)
1715 return((Image *) NULL);
1716 if (SetImageStorageClass(similarity_image,DirectClass) == MagickFalse)
1718 InheritException(exception,&similarity_image->exception);
1719 similarity_image=DestroyImage(similarity_image);
1720 return((Image *) NULL);
1723 Measure similarity of reference image against image.
1727 reference_statistics=GetImageChannelStatistics(reference,exception);
1728 similarity_view=AcquireCacheView(similarity_image);
1729 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1730 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
1732 for (y=0; y < (ssize_t) (image->rows-reference->rows+1); y++)
1740 register PixelPacket
1743 if (status == MagickFalse)
1745 q=GetCacheViewAuthenticPixels(similarity_view,0,y,similarity_image->columns,
1747 if (q == (const PixelPacket *) NULL)
1752 for (x=0; x < (ssize_t) (image->columns-reference->columns+1); x++)
1754 similarity=GetSimilarityMetric(image,reference,reference_statistics,x,y,
1756 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1757 #pragma omp critical (MagickCore_SimilarityImage)
1759 if (similarity < *similarity_metric)
1761 *similarity_metric=similarity;
1765 q->red=ClampToQuantum(QuantumRange-QuantumRange*similarity);
1770 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
1772 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1777 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1778 #pragma omp critical (MagickCore_SimilarityImage)
1780 proceed=SetImageProgress(image,SimilarityImageTag,progress++,
1782 if (proceed == MagickFalse)
1786 similarity_view=DestroyCacheView(similarity_view);
1787 reference_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1788 reference_statistics);
1789 return(similarity_image);