43#include "MagickCore/studio.h"
44#include "MagickCore/artifact.h"
45#include "MagickCore/attribute.h"
46#include "MagickCore/cache-view.h"
47#include "MagickCore/channel.h"
48#include "MagickCore/client.h"
49#include "MagickCore/color.h"
50#include "MagickCore/color-private.h"
51#include "MagickCore/colorspace.h"
52#include "MagickCore/colorspace-private.h"
53#include "MagickCore/compare.h"
54#include "MagickCore/compare-private.h"
55#include "MagickCore/composite-private.h"
56#include "MagickCore/constitute.h"
57#include "MagickCore/distort.h"
58#include "MagickCore/exception-private.h"
59#include "MagickCore/enhance.h"
60#include "MagickCore/fourier.h"
61#include "MagickCore/geometry.h"
62#include "MagickCore/image-private.h"
63#include "MagickCore/list.h"
64#include "MagickCore/log.h"
65#include "MagickCore/memory_.h"
66#include "MagickCore/monitor.h"
67#include "MagickCore/monitor-private.h"
68#include "MagickCore/option.h"
69#include "MagickCore/pixel-accessor.h"
70#include "MagickCore/property.h"
71#include "MagickCore/registry.h"
72#include "MagickCore/resource_.h"
73#include "MagickCore/string_.h"
74#include "MagickCore/statistic.h"
75#include "MagickCore/statistic-private.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/thread-private.h"
78#include "MagickCore/threshold.h"
79#include "MagickCore/transform.h"
80#include "MagickCore/utility.h"
81#include "MagickCore/version.h"
115MagickExport Image *CompareImages(Image *image,
const Image *reconstruct_image,
116 const MetricType metric,
double *distortion,ExceptionInfo *exception)
149 assert(image != (Image *) NULL);
150 assert(image->signature == MagickCoreSignature);
151 assert(reconstruct_image != (
const Image *) NULL);
152 assert(reconstruct_image->signature == MagickCoreSignature);
153 assert(distortion != (
double *) NULL);
154 if (IsEventLogging() != MagickFalse)
155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
157 status=GetImageDistortion(image,reconstruct_image,metric,distortion,
159 if (status == MagickFalse)
160 return((Image *) NULL);
161 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
162 SetGeometry(image,&geometry);
163 geometry.width=columns;
164 geometry.height=rows;
165 clone_image=CloneImage(image,0,0,MagickTrue,exception);
166 if (clone_image == (Image *) NULL)
167 return((Image *) NULL);
168 (void) SetImageMask(clone_image,ReadPixelMask,(Image *) NULL,exception);
169 difference_image=ExtentImage(clone_image,&geometry,exception);
170 clone_image=DestroyImage(clone_image);
171 if (difference_image == (Image *) NULL)
172 return((Image *) NULL);
173 (void) ResetImagePage(difference_image,
"0x0+0+0");
174 (void) SetImageAlphaChannel(difference_image,OpaqueAlphaChannel,exception);
175 highlight_image=CloneImage(image,columns,rows,MagickTrue,exception);
176 if (highlight_image == (Image *) NULL)
178 difference_image=DestroyImage(difference_image);
179 return((Image *) NULL);
181 status=SetImageStorageClass(highlight_image,DirectClass,exception);
182 if (status == MagickFalse)
184 difference_image=DestroyImage(difference_image);
185 highlight_image=DestroyImage(highlight_image);
186 return((Image *) NULL);
188 (void) SetImageMask(highlight_image,ReadPixelMask,(Image *) NULL,exception);
189 (void) SetImageAlphaChannel(highlight_image,OpaqueAlphaChannel,exception);
190 (void) QueryColorCompliance(
"#f1001ecc",AllCompliance,&highlight,exception);
191 artifact=GetImageArtifact(image,
"compare:highlight-color");
192 if (artifact != (
const char *) NULL)
193 (void) QueryColorCompliance(artifact,AllCompliance,&highlight,exception);
194 (void) QueryColorCompliance(
"#ffffffcc",AllCompliance,&lowlight,exception);
195 artifact=GetImageArtifact(image,
"compare:lowlight-color");
196 if (artifact != (
const char *) NULL)
197 (void) QueryColorCompliance(artifact,AllCompliance,&lowlight,exception);
198 (void) QueryColorCompliance(
"#888888cc",AllCompliance,&masklight,exception);
199 artifact=GetImageArtifact(image,
"compare:masklight-color");
200 if (artifact != (
const char *) NULL)
201 (void) QueryColorCompliance(artifact,AllCompliance,&masklight,exception);
205 image_view=AcquireVirtualCacheView(image,exception);
206 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
207 highlight_view=AcquireAuthenticCacheView(highlight_image,exception);
208#if defined(MAGICKCORE_OPENMP_SUPPORT)
209 #pragma omp parallel for schedule(static) shared(status) \
210 magick_number_threads(image,highlight_image,rows,1)
212 for (y=0; y < (ssize_t) rows; y++)
227 if (status == MagickFalse)
229 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
230 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
231 r=QueueCacheViewAuthenticPixels(highlight_view,0,y,columns,1,exception);
232 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL) ||
233 (r == (Quantum *) NULL))
238 for (x=0; x < (ssize_t) columns; x++)
240 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
241 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
243 SetPixelViaPixelInfo(highlight_image,&masklight,r);
244 p+=(ptrdiff_t) GetPixelChannels(image);
245 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
246 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
249 if (IsFuzzyEquivalencePixel(image,p,reconstruct_image,q) == MagickFalse)
250 SetPixelViaPixelInfo(highlight_image,&highlight,r);
252 SetPixelViaPixelInfo(highlight_image,&lowlight,r);
253 p+=(ptrdiff_t) GetPixelChannels(image);
254 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
255 r+=(ptrdiff_t) GetPixelChannels(highlight_image);
257 sync=SyncCacheViewAuthenticPixels(highlight_view,exception);
258 if (sync == MagickFalse)
261 highlight_view=DestroyCacheView(highlight_view);
262 reconstruct_view=DestroyCacheView(reconstruct_view);
263 image_view=DestroyCacheView(image_view);
264 if ((status != MagickFalse) && (difference_image != (Image *) NULL))
265 status=CompositeImage(difference_image,highlight_image,image->compose,
266 MagickTrue,0,0,exception);
267 highlight_image=DestroyImage(highlight_image);
268 if (status == MagickFalse)
269 difference_image=DestroyImage(difference_image);
270 return(difference_image);
307static MagickBooleanType GetAESimilarity(
const Image *image,
308 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
333 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
334 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
335 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
336 image_view=AcquireVirtualCacheView(image,exception);
337 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
338#if defined(MAGICKCORE_OPENMP_SUPPORT)
339 #pragma omp parallel for schedule(static) shared(similarity,status) \
340 magick_number_threads(image,image,rows,1)
342 for (y=0; y < (ssize_t) rows; y++)
349 channel_similarity[MaxPixelChannels+1] = { 0.0 };
354 if (status == MagickFalse)
356 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
357 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
358 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
363 for (x=0; x < (ssize_t) columns; x++)
372 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
373 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
375 p+=(ptrdiff_t) GetPixelChannels(image);
376 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
379 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
380 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
381 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
386 PixelChannel channel = GetPixelChannelChannel(image,i);
387 PixelTrait traits = GetPixelChannelTraits(image,channel);
388 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
390 if (((traits & UpdatePixelTrait) == 0) ||
391 ((reconstruct_traits & UpdatePixelTrait) == 0))
393 if (channel == AlphaPixelChannel)
394 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
397 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
398 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
400 double ae = fabs(QuantumScale*error);
401 channel_similarity[i]+=ae;
402 channel_similarity[CompositePixelChannel]+=ae;
405 p+=(ptrdiff_t) GetPixelChannels(image);
406 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
408#if defined(MAGICKCORE_OPENMP_SUPPORT)
409 #pragma omp critical (MagickCore_GetAESimilarity)
415 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
417 PixelChannel channel = GetPixelChannelChannel(image,j);
418 PixelTrait traits = GetPixelChannelTraits(image,channel);
419 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
421 if (((traits & UpdatePixelTrait) == 0) ||
422 ((reconstruct_traits & UpdatePixelTrait) == 0))
424 similarity[j]+=channel_similarity[j];
426 similarity[CompositePixelChannel]+=
427 channel_similarity[CompositePixelChannel];
430 reconstruct_view=DestroyCacheView(reconstruct_view);
431 image_view=DestroyCacheView(image_view);
432 area=MagickSafeReciprocal((
double) columns*rows);
433 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
435 PixelChannel channel = GetPixelChannelChannel(image,k);
436 PixelTrait traits = GetPixelChannelTraits(image,channel);
437 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
439 if (((traits & UpdatePixelTrait) == 0) ||
440 ((reconstruct_traits & UpdatePixelTrait) == 0))
445 similarity[CompositePixelChannel]*=area;
447 similarity[CompositePixelChannel]/=(double) channels;
451static MagickBooleanType GetDPCSimilarity(
const Image *image,
452 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
454#define SimilarityImageTag "Similarity/Image"
462 *reconstruct_statistics;
465 norm[MaxPixelChannels+1] = { 0.0 },
466 reconstruct_norm[MaxPixelChannels+1] = { 0.0 };
485 image_statistics=GetImageStatistics(image,exception);
486 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
487 if ((image_statistics == (ChannelStatistics *) NULL) ||
488 (reconstruct_statistics == (ChannelStatistics *) NULL))
490 if (image_statistics != (ChannelStatistics *) NULL)
491 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
493 if (reconstruct_statistics != (ChannelStatistics *) NULL)
494 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
495 reconstruct_statistics);
498 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
499 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
500 image_view=AcquireVirtualCacheView(image,exception);
501 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
502#if defined(MAGICKCORE_OPENMP_SUPPORT)
503 #pragma omp parallel for schedule(static) shared(norm,reconstruct_norm,similarity,status) \
504 magick_number_threads(image,image,rows,1)
506 for (y=0; y < (ssize_t) rows; y++)
513 channel_norm[MaxPixelChannels+1] = { 0.0 },
514 channel_reconstruct_norm[MaxPixelChannels+1] = { 0.0 },
515 channel_similarity[MaxPixelChannels+1] = { 0.0 };
520 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
521 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
522 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
527 for (x=0; x < (ssize_t) columns; x++)
536 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
537 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
539 p+=(ptrdiff_t) GetPixelChannels(image);
540 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
543 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
544 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
545 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
551 PixelChannel channel = GetPixelChannelChannel(image,i);
552 PixelTrait traits = GetPixelChannelTraits(image,channel);
553 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
555 if (((traits & UpdatePixelTrait) == 0) ||
556 ((reconstruct_traits & UpdatePixelTrait) == 0))
558 if (channel == AlphaPixelChannel)
560 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
561 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
562 channel,q)-reconstruct_statistics[channel].mean);
566 alpha=QuantumScale*(Sa*p[i]-image_statistics[channel].mean);
567 beta=QuantumScale*(Da*GetPixelChannel(reconstruct_image,channel,
568 q)-reconstruct_statistics[channel].mean);
570 channel_similarity[i]+=alpha*beta;
571 channel_norm[i]+=alpha*alpha;
572 channel_reconstruct_norm[i]+=beta*beta;
574 p+=(ptrdiff_t) GetPixelChannels(image);
575 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
577#if defined(MAGICKCORE_OPENMP_SUPPORT)
578 #pragma omp critical (MagickCore_GetDPCSimilarity)
584 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
586 PixelChannel channel = GetPixelChannelChannel(image,j);
587 PixelTrait traits = GetPixelChannelTraits(image,channel);
588 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
590 if (((traits & UpdatePixelTrait) == 0) ||
591 ((reconstruct_traits & UpdatePixelTrait) == 0))
593 similarity[j]+=channel_similarity[j];
594 similarity[CompositePixelChannel]+=channel_similarity[j];
595 norm[j]+=channel_norm[j];
596 norm[CompositePixelChannel]+=channel_norm[j];
597 reconstruct_norm[j]+=channel_reconstruct_norm[j];
598 reconstruct_norm[CompositePixelChannel]+=channel_reconstruct_norm[j];
601 if (image->progress_monitor != (MagickProgressMonitor) NULL)
606#if defined(MAGICKCORE_OPENMP_SUPPORT)
610 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
611 if (proceed == MagickFalse)
618 reconstruct_view=DestroyCacheView(reconstruct_view);
619 image_view=DestroyCacheView(image_view);
623 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
625 PixelChannel channel = GetPixelChannelChannel(image,k);
626 PixelTrait traits = GetPixelChannelTraits(image,channel);
627 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
629 if (((traits & UpdatePixelTrait) == 0) ||
630 ((reconstruct_traits & UpdatePixelTrait) == 0))
632 similarity[k]*=MagickSafeReciprocal(sqrt(norm[k]*reconstruct_norm[k]));
634 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
635 norm[CompositePixelChannel]*reconstruct_norm[CompositePixelChannel]));
639 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
640 reconstruct_statistics);
641 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
646static MagickBooleanType GetFUZZSimilarity(
const Image *image,
647 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
671 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
672 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
673 image_view=AcquireVirtualCacheView(image,exception);
674 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
675#if defined(MAGICKCORE_OPENMP_SUPPORT)
676 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
677 magick_number_threads(image,image,rows,1)
679 for (y=0; y < (ssize_t) rows; y++)
687 channel_similarity[MaxPixelChannels+1] = { 0.0 };
692 if (status == MagickFalse)
694 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
695 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
696 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
701 for (x=0; x < (ssize_t) columns; x++)
710 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
711 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
713 p+=(ptrdiff_t) GetPixelChannels(image);
714 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
717 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
718 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
719 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
724 PixelChannel channel = GetPixelChannelChannel(image,i);
725 PixelTrait traits = GetPixelChannelTraits(image,channel);
726 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
728 if (((traits & UpdatePixelTrait) == 0) ||
729 ((reconstruct_traits & UpdatePixelTrait) == 0))
731 if (channel == AlphaPixelChannel)
732 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
735 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
736 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
738 channel_similarity[i]+=QuantumScale*error*QuantumScale*error;
739 channel_similarity[CompositePixelChannel]+=QuantumScale*error*
744 p+=(ptrdiff_t) GetPixelChannels(image);
745 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
747#if defined(MAGICKCORE_OPENMP_SUPPORT)
748 #pragma omp critical (MagickCore_GetFUZZSimilarity)
755 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
757 PixelChannel channel = GetPixelChannelChannel(image,j);
758 PixelTrait traits = GetPixelChannelTraits(image,channel);
759 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
761 if (((traits & UpdatePixelTrait) == 0) ||
762 ((reconstruct_traits & UpdatePixelTrait) == 0))
764 similarity[j]+=channel_similarity[j];
766 similarity[CompositePixelChannel]+=
767 channel_similarity[CompositePixelChannel];
770 reconstruct_view=DestroyCacheView(reconstruct_view);
771 image_view=DestroyCacheView(image_view);
772 area=MagickSafeReciprocal(area);
773 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
775 PixelChannel channel = GetPixelChannelChannel(image,k);
776 PixelTrait traits = GetPixelChannelTraits(image,channel);
777 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
779 if (((traits & UpdatePixelTrait) == 0) ||
780 ((reconstruct_traits & UpdatePixelTrait) == 0))
784 similarity[CompositePixelChannel]*=area;
788static MagickBooleanType GetMAESimilarity(
const Image *image,
789 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
813 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
814 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
815 image_view=AcquireVirtualCacheView(image,exception);
816 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
817#if defined(MAGICKCORE_OPENMP_SUPPORT)
818 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
819 magick_number_threads(image,image,rows,1)
821 for (y=0; y < (ssize_t) rows; y++)
829 channel_similarity[MaxPixelChannels+1] = { 0.0 };
834 if (status == MagickFalse)
836 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
837 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
838 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
843 for (x=0; x < (ssize_t) columns; x++)
852 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
853 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
855 p+=(ptrdiff_t) GetPixelChannels(image);
856 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
859 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
860 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
861 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
866 PixelChannel channel = GetPixelChannelChannel(image,i);
867 PixelTrait traits = GetPixelChannelTraits(image,channel);
868 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
870 if (((traits & UpdatePixelTrait) == 0) ||
871 ((reconstruct_traits & UpdatePixelTrait) == 0))
873 if (channel == AlphaPixelChannel)
874 error=QuantumScale*fabs((
double) p[i]-(
double) GetPixelChannel(
875 reconstruct_image,channel,q));
877 error=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
879 channel_similarity[i]+=error;
880 channel_similarity[CompositePixelChannel]+=error;
883 p+=(ptrdiff_t) GetPixelChannels(image);
884 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
886#if defined(MAGICKCORE_OPENMP_SUPPORT)
887 #pragma omp critical (MagickCore_GetMAESimilarity)
894 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
896 PixelChannel channel = GetPixelChannelChannel(image,j);
897 PixelTrait traits = GetPixelChannelTraits(image,channel);
898 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
900 if (((traits & UpdatePixelTrait) == 0) ||
901 ((reconstruct_traits & UpdatePixelTrait) == 0))
903 similarity[j]+=channel_similarity[j];
905 similarity[CompositePixelChannel]+=
906 channel_similarity[CompositePixelChannel];
909 reconstruct_view=DestroyCacheView(reconstruct_view);
910 image_view=DestroyCacheView(image_view);
911 area=MagickSafeReciprocal(area);
912 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
914 PixelChannel channel = GetPixelChannelChannel(image,k);
915 PixelTrait traits = GetPixelChannelTraits(image,channel);
916 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
918 if (((traits & UpdatePixelTrait) == 0) ||
919 ((reconstruct_traits & UpdatePixelTrait) == 0))
924 similarity[CompositePixelChannel]*=area;
926 similarity[CompositePixelChannel]/=(double) channels;
930static MagickBooleanType GetMEPPSimilarity(Image *image,
931 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
939 maximum_error = -MagickMaximumValue,
957 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
958 image_view=AcquireVirtualCacheView(image,exception);
959 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
960#if defined(MAGICKCORE_OPENMP_SUPPORT)
961 #pragma omp parallel for schedule(static) shared(area,similarity,maximum_error,mean_error,status) \
962 magick_number_threads(image,image,rows,1)
964 for (y=0; y < (ssize_t) rows; y++)
972 channel_similarity[MaxPixelChannels+1] = { 0.0 },
973 channel_maximum_error = maximum_error,
974 channel_mean_error = 0.0;
979 if (status == MagickFalse)
981 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
982 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
983 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
988 for (x=0; x < (ssize_t) columns; x++)
997 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
998 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1000 p+=(ptrdiff_t) GetPixelChannels(image);
1001 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1004 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1005 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1006 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1011 PixelChannel channel = GetPixelChannelChannel(image,i);
1012 PixelTrait traits = GetPixelChannelTraits(image,channel);
1013 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1015 if (((traits & UpdatePixelTrait) == 0) ||
1016 ((reconstruct_traits & UpdatePixelTrait) == 0))
1018 if (channel == AlphaPixelChannel)
1019 error=QuantumScale*fabs((
double) p[i]-(
double) GetPixelChannel(
1020 reconstruct_image,channel,q));
1022 error=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
1024 channel_similarity[i]+=error;
1025 channel_similarity[CompositePixelChannel]+=error;
1026 channel_mean_error+=error*error;
1027 if (error > channel_maximum_error)
1028 channel_maximum_error=error;
1031 p+=(ptrdiff_t) GetPixelChannels(image);
1032 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1034#if defined(MAGICKCORE_OPENMP_SUPPORT)
1035 #pragma omp critical (MagickCore_GetMEPPSimilarity)
1042 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1044 PixelChannel channel = GetPixelChannelChannel(image,j);
1045 PixelTrait traits = GetPixelChannelTraits(image,channel);
1046 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1048 if (((traits & UpdatePixelTrait) == 0) ||
1049 ((reconstruct_traits & UpdatePixelTrait) == 0))
1051 similarity[j]+=channel_similarity[j];
1053 similarity[CompositePixelChannel]+=
1054 channel_similarity[CompositePixelChannel];
1055 mean_error+=channel_mean_error;
1056 if (channel_maximum_error > maximum_error)
1057 maximum_error=channel_maximum_error;
1060 reconstruct_view=DestroyCacheView(reconstruct_view);
1061 image_view=DestroyCacheView(image_view);
1062 area=MagickSafeReciprocal(area);
1063 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1065 PixelChannel channel = GetPixelChannelChannel(image,k);
1066 PixelTrait traits = GetPixelChannelTraits(image,channel);
1067 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1069 if (((traits & UpdatePixelTrait) == 0) ||
1070 ((reconstruct_traits & UpdatePixelTrait) == 0))
1072 similarity[k]*=area;
1075 similarity[CompositePixelChannel]*=area;
1077 similarity[CompositePixelChannel]/=(double) channels;
1078 image->error.mean_error_per_pixel=QuantumRange*
1079 similarity[CompositePixelChannel];
1080 image->error.normalized_mean_error=mean_error*area;
1081 image->error.normalized_maximum_error=maximum_error;
1085static MagickBooleanType GetMSESimilarity(
const Image *image,
1086 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1096 status = MagickTrue;
1110 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1111 image_view=AcquireVirtualCacheView(image,exception);
1112 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1113#if defined(MAGICKCORE_OPENMP_SUPPORT)
1114 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1115 magick_number_threads(image,image,rows,1)
1117 for (y=0; y < (ssize_t) rows; y++)
1125 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1130 if (status == MagickFalse)
1132 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1133 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1134 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1139 for (x=0; x < (ssize_t) columns; x++)
1148 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1149 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1151 p+=(ptrdiff_t) GetPixelChannels(image);
1152 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1155 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1156 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1157 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1162 PixelChannel channel = GetPixelChannelChannel(image,i);
1163 PixelTrait traits = GetPixelChannelTraits(image,channel);
1164 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1166 if (((traits & UpdatePixelTrait) == 0) ||
1167 ((reconstruct_traits & UpdatePixelTrait) == 0))
1169 if (channel == AlphaPixelChannel)
1170 error=QuantumScale*((double) p[i]-(double) GetPixelChannel(
1171 reconstruct_image,channel,q));
1173 error=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
1175 channel_similarity[i]+=error*error;
1176 channel_similarity[CompositePixelChannel]+=error*error;
1179 p+=(ptrdiff_t) GetPixelChannels(image);
1180 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1182#if defined(MAGICKCORE_OPENMP_SUPPORT)
1183 #pragma omp critical (MagickCore_GetMSESimilarity)
1190 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1192 PixelChannel channel = GetPixelChannelChannel(image,j);
1193 PixelTrait traits = GetPixelChannelTraits(image,channel);
1194 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1196 if (((traits & UpdatePixelTrait) == 0) ||
1197 ((reconstruct_traits & UpdatePixelTrait) == 0))
1199 similarity[j]+=channel_similarity[j];
1201 similarity[CompositePixelChannel]+=
1202 channel_similarity[CompositePixelChannel];
1205 reconstruct_view=DestroyCacheView(reconstruct_view);
1206 image_view=DestroyCacheView(image_view);
1207 area=MagickSafeReciprocal(area);
1208 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1210 PixelChannel channel = GetPixelChannelChannel(image,k);
1211 PixelTrait traits = GetPixelChannelTraits(image,channel);
1212 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1214 if (((traits & UpdatePixelTrait) == 0) ||
1215 ((reconstruct_traits & UpdatePixelTrait) == 0))
1217 similarity[k]*=area;
1220 similarity[CompositePixelChannel]*=area;
1222 similarity[CompositePixelChannel]/=(double) channels;
1226static MagickBooleanType GetNCCSimilarity(
const Image *image,
1227 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1235 *reconstruct_statistics;
1238 reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1239 variance[MaxPixelChannels+1] = { 0.0 };
1242 status = MagickTrue;
1258 image_statistics=GetImageStatistics(image,exception);
1259 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
1260 if ((image_statistics == (ChannelStatistics *) NULL) ||
1261 (reconstruct_statistics == (ChannelStatistics *) NULL))
1263 if (image_statistics != (ChannelStatistics *) NULL)
1264 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1266 if (reconstruct_statistics != (ChannelStatistics *) NULL)
1267 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1268 reconstruct_statistics);
1269 return(MagickFalse);
1271 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1272 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1273 image_view=AcquireVirtualCacheView(image,exception);
1274 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1275#if defined(MAGICKCORE_OPENMP_SUPPORT)
1276 #pragma omp parallel for schedule(static) shared(variance,reconstruct_variance,similarity,status) \
1277 magick_number_threads(image,image,rows,1)
1279 for (y=0; y < (ssize_t) rows; y++)
1286 channel_reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1287 channel_similarity[MaxPixelChannels+1] = { 0.0 },
1288 channel_variance[MaxPixelChannels+1] = { 0.0 };
1293 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1294 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1295 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1300 for (x=0; x < (ssize_t) columns; x++)
1309 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1310 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1312 p+=(ptrdiff_t) GetPixelChannels(image);
1313 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1316 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1317 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1318 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1324 PixelChannel channel = GetPixelChannelChannel(image,i);
1325 PixelTrait traits = GetPixelChannelTraits(image,channel);
1326 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1328 if (((traits & UpdatePixelTrait) == 0) ||
1329 ((reconstruct_traits & UpdatePixelTrait) == 0))
1331 if (channel == AlphaPixelChannel)
1333 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
1334 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
1335 channel,q)-reconstruct_statistics[channel].mean);
1339 alpha=QuantumScale*(Sa*p[i]-image_statistics[channel].mean);
1340 beta=QuantumScale*(Da*GetPixelChannel(reconstruct_image,channel,
1341 q)-reconstruct_statistics[channel].mean);
1343 channel_similarity[i]+=alpha*beta;
1344 channel_variance[i]+=alpha*alpha;
1345 channel_reconstruct_variance[i]+=beta*beta;
1347 p+=(ptrdiff_t) GetPixelChannels(image);
1348 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1350#if defined(MAGICKCORE_OPENMP_SUPPORT)
1351 #pragma omp critical (MagickCore_GetNCCSimilarity)
1357 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1359 PixelChannel channel = GetPixelChannelChannel(image,j);
1360 PixelTrait traits = GetPixelChannelTraits(image,channel);
1361 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1363 if (((traits & UpdatePixelTrait) == 0) ||
1364 ((reconstruct_traits & UpdatePixelTrait) == 0))
1366 similarity[j]+=channel_similarity[j];
1367 similarity[CompositePixelChannel]+=channel_similarity[j];
1368 variance[j]+=channel_variance[j];
1369 variance[CompositePixelChannel]+=channel_variance[j];
1370 reconstruct_variance[j]+=channel_reconstruct_variance[j];
1371 reconstruct_variance[CompositePixelChannel]+=
1372 channel_reconstruct_variance[j];
1375 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1380#if defined(MAGICKCORE_OPENMP_SUPPORT)
1384 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1385 if (proceed == MagickFalse)
1392 reconstruct_view=DestroyCacheView(reconstruct_view);
1393 image_view=DestroyCacheView(image_view);
1397 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1399 PixelChannel channel = GetPixelChannelChannel(image,k);
1400 PixelTrait traits = GetPixelChannelTraits(image,channel);
1401 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1403 if (((traits & UpdatePixelTrait) == 0) ||
1404 ((reconstruct_traits & UpdatePixelTrait) == 0))
1406 similarity[k]*=MagickSafeReciprocal(sqrt(variance[k])*
1407 sqrt(reconstruct_variance[k]));
1409 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
1410 variance[CompositePixelChannel])*sqrt(
1411 reconstruct_variance[CompositePixelChannel]));
1415 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1416 reconstruct_statistics);
1417 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1422static MagickBooleanType GetPASimilarity(
const Image *image,
1423 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1430 status = MagickTrue;
1442 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1443 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1444 image_view=AcquireVirtualCacheView(image,exception);
1445 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1446#if defined(MAGICKCORE_OPENMP_SUPPORT)
1447 #pragma omp parallel for schedule(static) shared(similarity,status) \
1448 magick_number_threads(image,image,rows,1)
1450 for (y=0; y < (ssize_t) rows; y++)
1457 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1462 if (status == MagickFalse)
1464 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1465 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1466 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1471 for (x=0; x < (ssize_t) columns; x++)
1480 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1481 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1483 p+=(ptrdiff_t) GetPixelChannels(image);
1484 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1487 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1488 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1489 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1494 PixelChannel channel = GetPixelChannelChannel(image,i);
1495 PixelTrait traits = GetPixelChannelTraits(image,channel);
1496 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1498 if (((traits & UpdatePixelTrait) == 0) ||
1499 ((reconstruct_traits & UpdatePixelTrait) == 0))
1501 if (channel == AlphaPixelChannel)
1502 distance=QuantumScale*fabs((
double) p[i]-(
double)
1503 GetPixelChannel(reconstruct_image,channel,q));
1505 distance=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(
1506 reconstruct_image,channel,q));
1507 if (distance > channel_similarity[i])
1508 channel_similarity[i]=distance;
1509 if (distance > channel_similarity[CompositePixelChannel])
1510 channel_similarity[CompositePixelChannel]=distance;
1512 p+=(ptrdiff_t) GetPixelChannels(image);
1513 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1515#if defined(MAGICKCORE_OPENMP_SUPPORT)
1516 #pragma omp critical (MagickCore_GetPASimilarity)
1522 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1524 PixelChannel channel = GetPixelChannelChannel(image,j);
1525 PixelTrait traits = GetPixelChannelTraits(image,channel);
1526 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1528 if (((traits & UpdatePixelTrait) == 0) ||
1529 ((reconstruct_traits & UpdatePixelTrait) == 0))
1531 if (channel_similarity[j] > similarity[j])
1532 similarity[j]=channel_similarity[j];
1534 if (channel_similarity[CompositePixelChannel] > similarity[CompositePixelChannel])
1535 similarity[CompositePixelChannel]=
1536 channel_similarity[CompositePixelChannel];
1539 reconstruct_view=DestroyCacheView(reconstruct_view);
1540 image_view=DestroyCacheView(image_view);
1544static MagickBooleanType GetPDCSimilarity(
const Image *image,
1545 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1555 status = MagickTrue;
1567 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
1568 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1569 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1570 image_view=AcquireVirtualCacheView(image,exception);
1571 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1572#if defined(MAGICKCORE_OPENMP_SUPPORT)
1573 #pragma omp parallel for schedule(static) shared(similarity,status) \
1574 magick_number_threads(image,image,rows,1)
1576 for (y=0; y < (ssize_t) rows; y++)
1583 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1588 if (status == MagickFalse)
1590 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1591 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1592 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1597 for (x=0; x < (ssize_t) columns; x++)
1609 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1610 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1612 p+=(ptrdiff_t) GetPixelChannels(image);
1613 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1616 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1617 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1618 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1623 PixelChannel channel = GetPixelChannelChannel(image,i);
1624 PixelTrait traits = GetPixelChannelTraits(image,channel);
1625 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1627 if (((traits & UpdatePixelTrait) == 0) ||
1628 ((reconstruct_traits & UpdatePixelTrait) == 0))
1630 if (channel == AlphaPixelChannel)
1631 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
1634 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
1635 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
1637 channel_similarity[i]++;
1642 channel_similarity[CompositePixelChannel]++;
1643 p+=(ptrdiff_t) GetPixelChannels(image);
1644 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1646#if defined(MAGICKCORE_OPENMP_SUPPORT)
1647 #pragma omp critical (MagickCore_GetPDCSimilarity)
1653 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1655 PixelChannel channel = GetPixelChannelChannel(image,j);
1656 PixelTrait traits = GetPixelChannelTraits(image,channel);
1657 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1659 if (((traits & UpdatePixelTrait) == 0) ||
1660 ((reconstruct_traits & UpdatePixelTrait) == 0))
1662 similarity[j]+=channel_similarity[j];
1664 similarity[CompositePixelChannel]+=
1665 channel_similarity[CompositePixelChannel];
1668 reconstruct_view=DestroyCacheView(reconstruct_view);
1669 image_view=DestroyCacheView(image_view);
1673static Image *GetEdgeCorrelationSurface(
const Image *image,
1674 ExceptionInfo *exception)
1685 kernel=AcquireKernelInfo(
"LoG:0,1",exception);
1686 if (kernel == (KernelInfo *) NULL)
1687 return((Image *) NULL);
1688 surface=MorphologyImage(image,ConvolveMorphology,1,kernel,exception);
1689 kernel=DestroyKernelInfo(kernel);
1693static MagickBooleanType GetPHASESimilarity(
const Image *image,
1694 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1697 *edge_reconstruct_view,
1704 correlation[MaxPixelChannels+1] = { 0.0 },
1705 image_sum[MaxPixelChannels+1] = { 0.0 },
1706 image_sum_squared[MaxPixelChannels+1] = { 0.0 },
1707 reconstruct_sum[MaxPixelChannels+1] = { 0.0 },
1708 reconstruct_sum_squared[MaxPixelChannels+1] = { 0.0 };
1715 status = MagickTrue;
1730 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1731 edge_image=GetEdgeCorrelationSurface(image,exception);
1732 edge_reconstruct=GetEdgeCorrelationSurface(reconstruct_image,exception);
1733 if ((edge_image == (Image *) NULL) || (edge_reconstruct == (Image *) NULL))
1735 if (edge_image != (Image *) NULL)
1736 edge_image=DestroyImage(edge_image);
1737 if (edge_reconstruct != (Image *) NULL)
1738 edge_reconstruct=DestroyImage(edge_reconstruct);
1739 return(MagickFalse);
1741 image_view=AcquireVirtualCacheView(image,exception);
1742 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1743 edge_view=AcquireVirtualCacheView(edge_image,exception);
1744 edge_reconstruct_view=AcquireVirtualCacheView(edge_reconstruct,exception);
1745#if defined(MAGICKCORE_OPENMP_SUPPORT)
1746 #pragma omp parallel for schedule(static) shared(status) \
1747 magick_number_threads(edge_image,edge_reconstruct,rows,1)
1749 for (y=0; y < (ssize_t) rows; y++)
1753 *magick_restrict pm,
1755 *magick_restrict qm;
1759 channel_correlation[MaxPixelChannels+1] = { 0.0 },
1760 channel_image_sum[MaxPixelChannels+1] = { 0.0 },
1761 channel_image_sum_squared[MaxPixelChannels+1] = { 0.0 },
1762 channel_reconstruct_sum[MaxPixelChannels+1] = { 0.0 },
1763 channel_reconstruct_sum_squared[MaxPixelChannels+1] = { 0.0 };
1769 if (status == MagickFalse)
1771 pm=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1772 qm=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1773 p=GetCacheViewVirtualPixels(edge_view,0,y,columns,1,exception);
1774 q=GetCacheViewVirtualPixels(edge_reconstruct_view,0,y,columns,1,exception);
1775 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL) ||
1776 (pm == (
const Quantum *) NULL) || (qm == (
const Quantum *) NULL))
1781 for (x=0; x < (ssize_t) columns; x++)
1783 if ((GetPixelReadMask(image,pm) <= (QuantumRange/2)) ||
1784 (GetPixelReadMask(reconstruct_image,qm) <= (QuantumRange/2)))
1786 p+=(ptrdiff_t) GetPixelChannels(edge_image);
1787 q+=(ptrdiff_t) GetPixelChannels(edge_reconstruct);
1788 pm+=(ptrdiff_t) GetPixelChannels(image);
1789 qm+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1792 for (i=0; i < (ssize_t) GetPixelChannels(edge_image); i++)
1808 channel=GetPixelChannelChannel(edge_image,i);
1809 traits=GetPixelChannelTraits(edge_image,channel);
1810 reconstruct_traits=GetPixelChannelTraits(edge_reconstruct,channel);
1811 if (((traits & UpdatePixelTrait) == 0) ||
1812 ((reconstruct_traits & UpdatePixelTrait) == 0))
1814 offset=GetPixelChannelOffset(edge_reconstruct,channel);
1817 alpha=QuantumScale*(double) p[i];
1818 beta=QuantumScale*(double) q[offset];
1819 channel_image_sum[i]+=alpha;
1820 channel_image_sum_squared[i]+=alpha*alpha;
1821 channel_reconstruct_sum[i]+=beta;
1822 channel_reconstruct_sum_squared[i]+=beta*beta;
1823 channel_correlation[i]+=alpha*beta;
1826 p+=(ptrdiff_t) GetPixelChannels(edge_image);
1827 q+=(ptrdiff_t) GetPixelChannels(edge_reconstruct);
1828 pm+=(ptrdiff_t) GetPixelChannels(image);
1829 qm+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1831#if defined(MAGICKCORE_OPENMP_SUPPORT)
1832 #pragma omp critical (MagickCore_GetPHASESimilarity)
1836 for (i=0; i <= (ssize_t) MaxPixelChannels; i++)
1838 correlation[i]+=channel_correlation[i];
1839 image_sum[i]+=channel_image_sum[i];
1840 image_sum_squared[i]+=channel_image_sum_squared[i];
1841 reconstruct_sum[i]+=channel_reconstruct_sum[i];
1842 reconstruct_sum_squared[i]+=channel_reconstruct_sum_squared[i];
1846 edge_reconstruct_view=DestroyCacheView(edge_reconstruct_view);
1847 edge_view=DestroyCacheView(edge_view);
1848 reconstruct_view=DestroyCacheView(reconstruct_view);
1849 image_view=DestroyCacheView(image_view);
1850 edge_image=DestroyImage(edge_image);
1851 edge_reconstruct=DestroyImage(edge_reconstruct);
1852 if (status == MagickFalse)
1853 return(MagickFalse);
1856 (void) ThrowMagickException(exception,GetMagickModule(),ImageError,
1857 "InsufficientImageDataInRaster",
"`%s'",image->filename);
1858 return(MagickFalse);
1863 similarity[CompositePixelChannel]=0.0;
1864 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1871 reconstruct_variance;
1880 channel=GetPixelChannelChannel(image,j);
1881 traits=GetPixelChannelTraits(image,channel);
1882 reconstruct_traits=GetPixelChannelTraits(reconstruct_image,channel);
1883 if (((traits & UpdatePixelTrait) == 0) ||
1884 ((reconstruct_traits & UpdatePixelTrait) == 0))
1886 numerator=area*correlation[j]-image_sum[j]*reconstruct_sum[j];
1887 image_variance=area*image_sum_squared[j]-image_sum[j]*image_sum[j];
1888 reconstruct_variance=area*reconstruct_sum_squared[j]-reconstruct_sum[j]*
1890 if ((image_variance < MagickEpsilon) &&
1891 (reconstruct_variance < MagickEpsilon))
1892 pearson=(fabs(image_sum[j]-reconstruct_sum[j]) < MagickEpsilon) ?
1896 denominator=sqrt(image_variance)*sqrt(reconstruct_variance);
1897 pearson=denominator < MagickEpsilon ? 0.0 : numerator/denominator;
1903 similarity[j]=pearson;
1904 similarity[CompositePixelChannel]+=pearson;
1908 similarity[CompositePixelChannel]/=(double) channels;
1912static MagickBooleanType GetPHASHSimilarity(
const Image *image,
1913 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1915 ChannelPerceptualHash
1929 channel_phash=GetImagePerceptualHash(image,exception);
1930 if (channel_phash == (ChannelPerceptualHash *) NULL)
1931 return(MagickFalse);
1932 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1933 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1935 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1937 return(MagickFalse);
1939 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1947 PixelChannel channel = GetPixelChannelChannel(image,i);
1948 PixelTrait traits = GetPixelChannelTraits(image,channel);
1949 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1951 if (((traits & UpdatePixelTrait) == 0) ||
1952 ((reconstruct_traits & UpdatePixelTrait) == 0))
1954 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1963 for (k=0; k < MaximumNumberOfPerceptualHashes; k++)
1968 alpha=channel_phash[i].phash[j][k];
1969 beta=reconstruct_phash[i].phash[j][k];
1971 if (IsNaN(error) != 0)
1973 difference+=error*error;
1976 similarity[i]+=difference;
1977 similarity[CompositePixelChannel]+=difference;
1981 similarity[CompositePixelChannel]/=(double) channels;
1982 artifact=GetImageArtifact(image,
"phash:normalize");
1983 if (IsStringTrue(artifact) != MagickFalse)
1985 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1987 PixelChannel channel = GetPixelChannelChannel(image,i);
1988 PixelTrait traits = GetPixelChannelTraits(image,channel);
1989 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1991 if (((traits & UpdatePixelTrait) == 0) ||
1992 ((reconstruct_traits & UpdatePixelTrait) == 0))
1994 similarity[i]=sqrt(similarity[i]/channel_phash[0].number_colorspaces);
1996 similarity[CompositePixelChannel]=sqrt(similarity[CompositePixelChannel]/
1997 channel_phash[0].number_colorspaces);
2002 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
2004 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
2008static MagickBooleanType GetPSNRSimilarity(
const Image *image,
2009 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2012 status = MagickTrue;
2020 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
2021 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2023 PixelChannel channel = GetPixelChannelChannel(image,i);
2024 PixelTrait traits = GetPixelChannelTraits(image,channel);
2025 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2027 if (((traits & UpdatePixelTrait) == 0) ||
2028 ((reconstruct_traits & UpdatePixelTrait) == 0))
2030 similarity[i]=10.0*MagickSafeLog10(MagickSafeReciprocal(
2031 similarity[i]))/MagickSafePSNRRecipicol(10.0);
2033 similarity[CompositePixelChannel]=10.0*MagickSafeLog10(
2034 MagickSafeReciprocal(similarity[CompositePixelChannel]))/
2035 MagickSafePSNRRecipicol(10.0);
2039static MagickBooleanType GetRMSESimilarity(
const Image *image,
2040 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2042#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
2045 status = MagickTrue;
2053 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
2054 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2056 PixelChannel channel = GetPixelChannelChannel(image,i);
2057 PixelTrait traits = GetPixelChannelTraits(image,channel);
2058 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2060 if (((traits & UpdatePixelTrait) == 0) ||
2061 ((reconstruct_traits & UpdatePixelTrait) == 0))
2063 similarity[i]=RMSESquareRoot(similarity[i]);
2065 similarity[CompositePixelChannel]=RMSESquareRoot(
2066 similarity[CompositePixelChannel]);
2070static MagickBooleanType GetSSIMSimularity(
const Image *image,
2071 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2073#define SSIMRadius 5.0
2074#define SSIMSigma 1.5
2084 geometry[MagickPathExtent];
2100 status = MagickTrue;
2115 artifact=GetImageArtifact(image,
"compare:ssim-radius");
2116 if (artifact != (
const char *) NULL)
2117 radius=StringToDouble(artifact,(
char **) NULL);
2119 artifact=GetImageArtifact(image,
"compare:ssim-sigma");
2120 if (artifact != (
const char *) NULL)
2121 sigma=StringToDouble(artifact,(
char **) NULL);
2122 (void) FormatLocaleString(geometry,MagickPathExtent,
"gaussian:%.17gx%.17g",
2124 kernel_info=AcquireKernelInfo(geometry,exception);
2125 if (kernel_info == (KernelInfo *) NULL)
2126 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2128 c1=pow(SSIMK1*SSIML,2.0);
2129 artifact=GetImageArtifact(image,
"compare:ssim-k1");
2130 if (artifact != (
const char *) NULL)
2131 c1=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
2132 c2=pow(SSIMK2*SSIML,2.0);
2133 artifact=GetImageArtifact(image,
"compare:ssim-k2");
2134 if (artifact != (
const char *) NULL)
2135 c2=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
2136 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2137 image_view=AcquireVirtualCacheView(image,exception);
2138 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2139#if defined(MAGICKCORE_OPENMP_SUPPORT)
2140 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
2141 magick_number_threads(image,reconstruct_image,rows,1)
2143 for (y=0; y < (ssize_t) rows; y++)
2151 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2157 if (status == MagickFalse)
2159 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
2160 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2161 kernel_info->height,exception);
2162 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
2163 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2164 kernel_info->height,exception);
2165 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2170 for (x=0; x < (ssize_t) columns; x++)
2173 *magick_restrict reconstruct,
2174 *magick_restrict test;
2177 x_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2178 x_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 },
2179 xy_sigma[MaxPixelChannels+1] = { 0.0 },
2180 y_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2181 y_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 };
2189 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
2190 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
2192 p+=(ptrdiff_t) GetPixelChannels(image);
2193 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2196 k=kernel_info->values;
2199 for (v=0; v < (ssize_t) kernel_info->height; v++)
2204 for (u=0; u < (ssize_t) kernel_info->width; u++)
2206 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2212 PixelChannel channel = GetPixelChannelChannel(image,i);
2213 PixelTrait traits = GetPixelChannelTraits(image,channel);
2214 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2215 reconstruct_image,channel);
2216 if (((traits & UpdatePixelTrait) == 0) ||
2217 ((reconstruct_traits & UpdatePixelTrait) == 0))
2219 x_pixel=QuantumScale*(double) test[i];
2220 x_pixel_mu[i]+=(*k)*x_pixel;
2221 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
2222 y_pixel=QuantumScale*(double)
2223 GetPixelChannel(reconstruct_image,channel,reconstruct);
2224 y_pixel_mu[i]+=(*k)*y_pixel;
2225 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
2226 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
2229 test+=(ptrdiff_t) GetPixelChannels(image);
2230 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2232 test+=(ptrdiff_t) GetPixelChannels(image)*columns;
2233 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image)*columns;
2235 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2240 x_pixel_sigmas_squared,
2244 y_pixel_sigmas_squared;
2246 PixelChannel channel = GetPixelChannelChannel(image,i);
2247 PixelTrait traits = GetPixelChannelTraits(image,channel);
2248 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2249 reconstruct_image,channel);
2250 if (((traits & UpdatePixelTrait) == 0) ||
2251 ((reconstruct_traits & UpdatePixelTrait) == 0))
2253 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
2254 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
2255 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
2256 xy_sigmas=xy_sigma[i]-xy_mu;
2257 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
2258 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
2259 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))*
2260 MagickSafeReciprocal((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
2261 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
2262 channel_similarity[i]+=ssim;
2263 channel_similarity[CompositePixelChannel]+=ssim;
2265 p+=(ptrdiff_t) GetPixelChannels(image);
2266 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2269#if defined(MAGICKCORE_OPENMP_SUPPORT)
2270 #pragma omp critical (MagickCore_GetSSIMSimularity)
2277 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2279 PixelChannel channel = GetPixelChannelChannel(image,j);
2280 PixelTrait traits = GetPixelChannelTraits(image,channel);
2281 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2283 if (((traits & UpdatePixelTrait) == 0) ||
2284 ((reconstruct_traits & UpdatePixelTrait) == 0))
2286 similarity[j]+=channel_similarity[j];
2288 similarity[CompositePixelChannel]+=
2289 channel_similarity[CompositePixelChannel];
2292 image_view=DestroyCacheView(image_view);
2293 reconstruct_view=DestroyCacheView(reconstruct_view);
2294 kernel_info=DestroyKernelInfo(kernel_info);
2295 area=MagickSafeReciprocal(area);
2296 for (l=0; l < (ssize_t) GetPixelChannels(image); l++)
2298 PixelChannel channel = GetPixelChannelChannel(image,l);
2299 PixelTrait traits = GetPixelChannelTraits(image,channel);
2300 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2302 if (((traits & UpdatePixelTrait) == 0) ||
2303 ((reconstruct_traits & UpdatePixelTrait) == 0))
2305 similarity[l]*=area;
2308 similarity[CompositePixelChannel]*=area;
2310 similarity[CompositePixelChannel]/=(double) channels;
2314static MagickBooleanType GetDSSIMSimilarity(
const Image *image,
2315 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2318 status = MagickTrue;
2326 status=GetSSIMSimularity(image,reconstruct_image,similarity,exception);
2327 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2329 PixelChannel channel = GetPixelChannelChannel(image,i);
2330 PixelTrait traits = GetPixelChannelTraits(image,channel);
2331 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2333 if (((traits & UpdatePixelTrait) == 0) ||
2334 ((reconstruct_traits & UpdatePixelTrait) == 0))
2336 similarity[i]=(1.0-similarity[i])/2.0;
2338 similarity[CompositePixelChannel]=(1.0-similarity[CompositePixelChannel])/2.0;
2342MagickExport MagickBooleanType GetImageDistortion(Image *image,
2343 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
2344 ExceptionInfo *exception)
2346#define CompareMetricNotSupportedException "metric not supported"
2349 *channel_similarity;
2352 status = MagickTrue;
2357 assert(image != (Image *) NULL);
2358 assert(image->signature == MagickCoreSignature);
2359 assert(reconstruct_image != (
const Image *) NULL);
2360 assert(reconstruct_image->signature == MagickCoreSignature);
2361 assert(distortion != (
double *) NULL);
2362 if (IsEventLogging() != MagickFalse)
2363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2368 length=MaxPixelChannels+1UL;
2369 channel_similarity=(
double *) AcquireQuantumMemory(length,
2370 sizeof(*channel_similarity));
2371 if (channel_similarity == (
double *) NULL)
2372 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2373 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2376 case AbsoluteErrorMetric:
2378 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2382 case DotProductCorrelationErrorMetric:
2384 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2388 case FuzzErrorMetric:
2390 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2394 case MeanAbsoluteErrorMetric:
2396 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2400 case MeanErrorPerPixelErrorMetric:
2402 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2406 case MeanSquaredErrorMetric:
2408 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2412 case NormalizedCrossCorrelationErrorMetric:
2414 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2418 case PeakAbsoluteErrorMetric:
2420 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2424 case PeakSignalToNoiseRatioErrorMetric:
2426 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2430 case PerceptualHashErrorMetric:
2432 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2436 case PhaseCorrelationErrorMetric:
2438 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2442 case PixelDifferenceCountErrorMetric:
2444 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2448 case RootMeanSquaredErrorMetric:
2449 case UndefinedErrorMetric:
2452 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2456 case StructuralDissimilarityErrorMetric:
2458 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2462 case StructuralSimilarityErrorMetric:
2464 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2469 *distortion=channel_similarity[CompositePixelChannel];
2472 case DotProductCorrelationErrorMetric:
2473 case NormalizedCrossCorrelationErrorMetric:
2474 case PhaseCorrelationErrorMetric:
2475 case StructuralSimilarityErrorMetric:
2477 *distortion=(1.0-(*distortion))/2.0;
2482 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2483 if (fabs(*distortion) < MagickEpsilon)
2485 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2521MagickExport
double *GetImageDistortions(Image *image,
2522 const Image *reconstruct_image,
const MetricType metric,
2523 ExceptionInfo *exception)
2527 *channel_similarity;
2530 status = MagickTrue;
2538 assert(image != (Image *) NULL);
2539 assert(image->signature == MagickCoreSignature);
2540 assert(reconstruct_image != (
const Image *) NULL);
2541 assert(reconstruct_image->signature == MagickCoreSignature);
2542 if (IsEventLogging() != MagickFalse)
2543 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2547 length=MaxPixelChannels+1UL;
2548 channel_similarity=(
double *) AcquireQuantumMemory(length,
2549 sizeof(*channel_similarity));
2550 if (channel_similarity == (
double *) NULL)
2551 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2552 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2555 case AbsoluteErrorMetric:
2557 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2561 case DotProductCorrelationErrorMetric:
2563 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2567 case FuzzErrorMetric:
2569 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2573 case MeanAbsoluteErrorMetric:
2575 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2579 case MeanErrorPerPixelErrorMetric:
2581 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2585 case MeanSquaredErrorMetric:
2587 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2591 case NormalizedCrossCorrelationErrorMetric:
2593 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2597 case PeakAbsoluteErrorMetric:
2599 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2603 case PeakSignalToNoiseRatioErrorMetric:
2605 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2609 case PerceptualHashErrorMetric:
2611 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2615 case PhaseCorrelationErrorMetric:
2617 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2621 case PixelDifferenceCountErrorMetric:
2623 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2627 case RootMeanSquaredErrorMetric:
2628 case UndefinedErrorMetric:
2631 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2635 case StructuralDissimilarityErrorMetric:
2637 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2641 case StructuralSimilarityErrorMetric:
2643 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2648 if (status == MagickFalse)
2650 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2651 return((
double *) NULL);
2653 distortion=channel_similarity;
2656 case DotProductCorrelationErrorMetric:
2657 case NormalizedCrossCorrelationErrorMetric:
2658 case PhaseCorrelationErrorMetric:
2659 case StructuralSimilarityErrorMetric:
2661 for (i=0; i <= MaxPixelChannels; i++)
2662 distortion[i]=(1.0-distortion[i])/2.0;
2667 for (i=0; i <= MaxPixelChannels; i++)
2668 if (fabs(distortion[i]) < MagickEpsilon)
2670 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2671 distortion[CompositePixelChannel]);
2703MagickExport MagickBooleanType IsImagesEqual(
const Image *image,
2704 const Image *reconstruct_image,ExceptionInfo *exception)
2717 assert(image != (Image *) NULL);
2718 assert(image->signature == MagickCoreSignature);
2719 assert(reconstruct_image != (
const Image *) NULL);
2720 assert(reconstruct_image->signature == MagickCoreSignature);
2721 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2722 image_view=AcquireVirtualCacheView(image,exception);
2723 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2724 for (y=0; y < (ssize_t) rows; y++)
2733 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2734 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2735 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2737 for (x=0; x < (ssize_t) columns; x++)
2742 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2747 PixelChannel channel = GetPixelChannelChannel(image,i);
2748 PixelTrait traits = GetPixelChannelTraits(image,channel);
2749 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2751 if (((traits & UpdatePixelTrait) == 0) ||
2752 ((reconstruct_traits & UpdatePixelTrait) == 0))
2754 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
2756 if (distance >= MagickEpsilon)
2759 if (i < (ssize_t) GetPixelChannels(image))
2761 p+=(ptrdiff_t) GetPixelChannels(image);
2762 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2764 if (x < (ssize_t) columns)
2767 reconstruct_view=DestroyCacheView(reconstruct_view);
2768 image_view=DestroyCacheView(image_view);
2769 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
2821MagickExport MagickBooleanType SetImageColorMetric(Image *image,
2822 const Image *reconstruct_image,ExceptionInfo *exception)
2825 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2830 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2832 if (status == MagickFalse)
2833 return(MagickFalse);
2834 status=fabs(image->error.mean_error_per_pixel) < MagickEpsilon ?
2835 MagickTrue : MagickFalse;
2882#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2883static Image *SIMCrossCorrelationImage(
const Image *alpha_image,
2884 const Image *beta_image,ExceptionInfo *exception)
2887 *alpha_fft = (Image *) NULL,
2888 *beta_fft = (Image *) NULL,
2889 *complex_conjugate = (Image *) NULL,
2890 *complex_multiplication = (Image *) NULL,
2891 *cross_correlation = (Image *) NULL,
2892 *temp_image = (Image *) NULL;
2897 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2898 if (temp_image == (Image *) NULL)
2899 return((Image *) NULL);
2900 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2901 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2902 temp_image=DestroyImageList(temp_image);
2903 if (beta_fft == (Image *) NULL)
2904 return((Image *) NULL);
2908 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2909 beta_fft=DestroyImageList(beta_fft);
2910 if (complex_conjugate == (Image *) NULL)
2911 return((Image *) NULL);
2915 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2916 if (temp_image == (Image *) NULL)
2918 complex_conjugate=DestroyImageList(complex_conjugate);
2919 return((Image *) NULL);
2921 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2922 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2923 temp_image=DestroyImageList(temp_image);
2924 if (alpha_fft == (Image *) NULL)
2926 complex_conjugate=DestroyImageList(complex_conjugate);
2927 return((Image *) NULL);
2932 DisableCompositeClampUnlessSpecified(complex_conjugate);
2933 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2934 AppendImageToList(&complex_conjugate,alpha_fft);
2935 complex_multiplication=ComplexImages(complex_conjugate,
2936 MultiplyComplexOperator,exception);
2937 complex_conjugate=DestroyImageList(complex_conjugate);
2938 if (complex_multiplication == (Image *) NULL)
2939 return((Image *) NULL);
2943 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2944 complex_multiplication->next,MagickFalse,exception);
2945 complex_multiplication=DestroyImageList(complex_multiplication);
2946 return(cross_correlation);
2949static Image *SIMDerivativeImage(
const Image *image,
const char *kernel,
2950 ExceptionInfo *exception)
2958 kernel_info=AcquireKernelInfo(kernel,exception);
2959 if (kernel_info == (KernelInfo *) NULL)
2960 return((Image *) NULL);
2961 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2963 kernel_info=DestroyKernelInfo(kernel_info);
2964 return(derivative_image);
2967static Image *SIMDivideImage(
const Image *numerator_image,
2968 const Image *denominator_image,ExceptionInfo *exception)
2978 status = MagickTrue;
2986 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2987 if (divide_image == (Image *) NULL)
2988 return(divide_image);
2989 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2990 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2991#if defined(MAGICKCORE_OPENMP_SUPPORT)
2992 #pragma omp parallel for schedule(static) shared(status) \
2993 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
2995 for (y=0; y < (ssize_t) divide_image->rows; y++)
3006 if (status == MagickFalse)
3008 p=GetCacheViewVirtualPixels(denominator_view,0,y,
3009 denominator_image->columns,1,exception);
3010 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
3012 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3017 for (x=0; x < (ssize_t) divide_image->columns; x++)
3022 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
3024 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
3025 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
3026 PixelTrait denominator_traits = GetPixelChannelTraits(denominator_image,
3028 if (((traits & UpdatePixelTrait) == 0) ||
3029 ((denominator_traits & UpdatePixelTrait) == 0))
3031 q[i]=(Quantum) ((
double) q[i]*MagickSafeReciprocal(QuantumScale*
3032 (
double) GetPixelChannel(denominator_image,channel,p)));
3034 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
3035 q+=(ptrdiff_t) GetPixelChannels(divide_image);
3037 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
3040 denominator_view=DestroyCacheView(denominator_view);
3041 numerator_view=DestroyCacheView(numerator_view);
3042 if (status == MagickFalse)
3043 divide_image=DestroyImage(divide_image);
3044 return(divide_image);
3047static Image *SIMDivideByMagnitude(Image *image,Image *magnitude_image,
3048 const Image *source_image,ExceptionInfo *exception)
3057 divide_image=SIMDivideImage(image,magnitude_image,exception);
3058 if (divide_image == (Image *) NULL)
3059 return((Image *) NULL);
3060 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
3061 ÷_image->background_color);
3062 SetGeometry(source_image,&geometry);
3063 geometry.width=MagickMax(source_image->columns,divide_image->columns);
3064 geometry.height=MagickMax(source_image->rows,divide_image->rows);
3065 result_image=ExtentImage(divide_image,&geometry,exception);
3066 divide_image=DestroyImage(divide_image);
3067 return(result_image);
3070static MagickBooleanType SIMFilterImageNaNs(Image *image,
3071 ExceptionInfo *exception)
3077 status = MagickTrue;
3085 image_view=AcquireAuthenticCacheView(image,exception);
3086#if defined(MAGICKCORE_OPENMP_SUPPORT)
3087 #pragma omp parallel for schedule(static) shared(status) \
3088 magick_number_threads(image,image,image->rows,1)
3090 for (y=0; y < (ssize_t) image->rows; y++)
3098 if (status == MagickFalse)
3100 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3101 if (q == (Quantum *) NULL)
3106 for (x=0; x < (ssize_t) image->columns; x++)
3111 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3113 PixelChannel channel = GetPixelChannelChannel(image,i);
3114 PixelTrait traits = GetPixelChannelTraits(image,channel);
3115 if ((traits & UpdatePixelTrait) == 0)
3117 if (IsNaN((
double) q[i]) != 0)
3120 q+=(ptrdiff_t) GetPixelChannels(image);
3122 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3125 image_view=DestroyCacheView(image_view);
3129static Image *SIMSquareImage(
const Image *image,ExceptionInfo *exception)
3138 status = MagickTrue;
3146 square_image=CloneImage(image,0,0,MagickTrue,exception);
3147 if (square_image == (Image *) NULL)
3148 return(square_image);
3149 image_view=AcquireAuthenticCacheView(square_image,exception);
3150#if defined(MAGICKCORE_OPENMP_SUPPORT)
3151 #pragma omp parallel for schedule(static) shared(status) \
3152 magick_number_threads(square_image,square_image,square_image->rows,1)
3154 for (y=0; y < (ssize_t) square_image->rows; y++)
3162 if (status == MagickFalse)
3164 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
3166 if (q == (Quantum *) NULL)
3171 for (x=0; x < (ssize_t) square_image->columns; x++)
3176 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
3178 PixelChannel channel = GetPixelChannelChannel(square_image,i);
3179 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
3180 if ((traits & UpdatePixelTrait) == 0)
3182 q[i]=(Quantum) (QuantumScale*q[i]*q[i]);
3184 q+=(ptrdiff_t) GetPixelChannels(square_image);
3186 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3189 image_view=DestroyCacheView(image_view);
3190 if (status == MagickFalse)
3191 square_image=DestroyImage(square_image);
3192 return(square_image);
3195static Image *SIMMagnitudeImage(Image *alpha_image,Image *beta_image,
3196 ExceptionInfo *exception)
3204 status = MagickTrue;
3206 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
3207 xsq_image=SIMSquareImage(alpha_image,exception);
3208 if (xsq_image == (Image *) NULL)
3209 return((Image *) NULL);
3210 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
3211 ysq_image=SIMSquareImage(beta_image,exception);
3212 if (ysq_image == (Image *) NULL)
3214 xsq_image=DestroyImage(xsq_image);
3215 return((Image *) NULL);
3217 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
3219 magnitude_image=xsq_image;
3220 ysq_image=DestroyImage(ysq_image);
3221 if (status == MagickFalse)
3223 magnitude_image=DestroyImage(magnitude_image);
3224 return((Image *) NULL);
3226 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
3227 if (status == MagickFalse)
3229 magnitude_image=DestroyImage(magnitude_image);
3230 return (Image *) NULL;
3232 return(magnitude_image);
3235static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
3236 RectangleInfo *offset,ExceptionInfo *exception)
3255 status = MagickTrue;
3258 maxima_info = { -MagickMaximumValue, 0, 0 };
3266 image_view=AcquireVirtualCacheView(image,exception);
3267 q=GetCacheViewVirtualPixels(image_view,maxima_info.x,maxima_info.y,1,1,
3269 if (q != (
const Quantum *) NULL)
3270 maxima_info.maxima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3271#if defined(MAGICKCORE_OPENMP_SUPPORT)
3272 #pragma omp parallel for schedule(static) shared(maxima_info,status) \
3273 magick_number_threads(image,image,image->rows,1)
3275 for (y=0; y < (ssize_t) image->rows; y++)
3281 channel_maxima = { -MagickMaximumValue, 0, 0 };
3286 if (status == MagickFalse)
3288 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3289 if (p == (
const Quantum *) NULL)
3294 channel_maxima=maxima_info;
3295 for (x=0; x < (ssize_t) image->columns; x++)
3300 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3305 PixelChannel channel = GetPixelChannelChannel(image,i);
3306 PixelTrait traits = GetPixelChannelTraits(image,channel);
3307 if ((traits & UpdatePixelTrait) == 0)
3309 pixel=(double) p[i];
3310 if (IsNaN(pixel) != 0)
3312 if (pixel > channel_maxima.maxima)
3314 channel_maxima.maxima=(double) p[i];
3319 p+=(ptrdiff_t) GetPixelChannels(image);
3321#if defined(MAGICKCORE_OPENMP_SUPPORT)
3322 #pragma omp critical (MagickCore_SIMMaximaImage)
3324 if (channel_maxima.maxima > maxima_info.maxima)
3325 maxima_info=channel_maxima;
3327 image_view=DestroyCacheView(image_view);
3328 *maxima=maxima_info.maxima;
3329 offset->x=maxima_info.x;
3330 offset->y=maxima_info.y;
3334static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
3335 RectangleInfo *offset,ExceptionInfo *exception)
3354 status = MagickTrue;
3357 minima_info = { MagickMaximumValue, 0, 0 };
3365 image_view=AcquireVirtualCacheView(image,exception);
3366 q=GetCacheViewVirtualPixels(image_view,minima_info.x,minima_info.y,1,1,
3368 if (q != (
const Quantum *) NULL)
3369 minima_info.minima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3370#if defined(MAGICKCORE_OPENMP_SUPPORT)
3371 #pragma omp parallel for schedule(static) shared(minima_info,status) \
3372 magick_number_threads(image,image,image->rows,1)
3374 for (y=0; y < (ssize_t) image->rows; y++)
3380 channel_minima = { MagickMaximumValue, 0, 0 };
3385 if (status == MagickFalse)
3387 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3388 if (p == (
const Quantum *) NULL)
3393 channel_minima=minima_info;
3394 for (x=0; x < (ssize_t) image->columns; x++)
3399 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3404 PixelChannel channel = GetPixelChannelChannel(image,i);
3405 PixelTrait traits = GetPixelChannelTraits(image,channel);
3406 if ((traits & UpdatePixelTrait) == 0)
3408 pixel=(double) p[i];
3409 if (IsNaN(pixel) != 0)
3411 if (pixel < channel_minima.minima)
3413 channel_minima.minima=pixel;
3418 p+=(ptrdiff_t) GetPixelChannels(image);
3420#if defined(MAGICKCORE_OPENMP_SUPPORT)
3421 #pragma omp critical (MagickCore_SIMMinimaImage)
3423 if (channel_minima.minima < minima_info.minima)
3424 minima_info=channel_minima;
3426 image_view=DestroyCacheView(image_view);
3427 *minima=minima_info.minima;
3428 offset->x=minima_info.x;
3429 offset->y=minima_info.y;
3433static MagickBooleanType SIMMultiplyImage(Image *image,
const double factor,
3434 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3440 status = MagickTrue;
3448 image_view=AcquireAuthenticCacheView(image,exception);
3449#if defined(MAGICKCORE_OPENMP_SUPPORT)
3450 #pragma omp parallel for schedule(static) shared(status) \
3451 magick_number_threads(image,image,image->rows,1)
3453 for (y=0; y < (ssize_t) image->rows; y++)
3461 if (status == MagickFalse)
3463 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3464 if (q == (Quantum *) NULL)
3469 for (x=0; x < (ssize_t) image->columns; x++)
3474 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3476 PixelChannel channel = GetPixelChannelChannel(image,i);
3477 PixelTrait traits = GetPixelChannelTraits(image,channel);
3478 if ((traits & UpdatePixelTrait) == 0)
3480 if (channel_statistics != (
const ChannelStatistics *) NULL)
3481 q[i]=(Quantum) (factor*q[i]*QuantumScale*
3482 channel_statistics[channel].standard_deviation);
3484 q[i]=(Quantum) (factor*q[i]);
3486 q+=(ptrdiff_t) GetPixelChannels(image);
3488 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3491 image_view=DestroyCacheView(image_view);
3495static Image *SIMPhaseCorrelationImage(
const Image *target_image,
3496 const Image *reconstruct_image,
const Image *magnitude_image,
3497 ExceptionInfo *exception)
3500 *target_fft = (Image *) NULL,
3501 *reconstruct_fft = (Image *) NULL,
3502 *complex_multiplication = (Image *) NULL,
3503 *cross_correlation = (Image *) NULL;
3508 reconstruct_fft=CloneImage(reconstruct_image,0,0,MagickTrue,exception);
3509 if (reconstruct_fft == NULL)
3510 return((Image *) NULL);
3511 (void) SetImageArtifact(reconstruct_fft,
"fourier:normalize",
"inverse");
3512 reconstruct_fft=ForwardFourierTransformImage(reconstruct_fft,MagickFalse,
3514 if (reconstruct_fft == NULL)
3515 return((Image *) NULL);
3519 target_fft=CloneImage(target_image,0,0,MagickTrue,exception);
3520 if (target_fft == (Image *) NULL)
3522 reconstruct_fft=DestroyImageList(reconstruct_fft);
3523 return((Image *) NULL);
3525 (void) SetImageArtifact(target_fft,
"fourier:normalize",
"inverse");
3526 target_fft=ForwardFourierTransformImage(target_fft,MagickFalse,exception);
3527 if (target_fft == (Image *) NULL)
3529 reconstruct_fft=DestroyImageList(reconstruct_fft);
3530 return((Image *) NULL);
3535 reconstruct_fft=ComplexImages(reconstruct_fft,ConjugateComplexOperator,
3537 if (reconstruct_fft == (Image *) NULL)
3539 target_fft=DestroyImageList(target_fft);
3540 return((Image *) NULL);
3545 AppendImageToList(&reconstruct_fft,target_fft);
3546 DisableCompositeClampUnlessSpecified(reconstruct_fft);
3547 DisableCompositeClampUnlessSpecified(reconstruct_fft->next);
3548 complex_multiplication=ComplexImages(reconstruct_fft,MultiplyComplexOperator,
3550 reconstruct_fft=DestroyImageList(reconstruct_fft);
3551 if (complex_multiplication == (Image *) NULL)
3552 return((Image *) NULL);
3553 if (complex_multiplication->next != (Image *) NULL)
3558 DisableCompositeClampUnlessSpecified(complex_multiplication);
3559 DisableCompositeClampUnlessSpecified(complex_multiplication->next);
3560 (void) CompositeImage(complex_multiplication,magnitude_image,
3561 DivideSrcCompositeOp,MagickTrue,0,0,exception);
3562 (void) CompositeImage(complex_multiplication->next,magnitude_image,
3563 DivideSrcCompositeOp,MagickTrue,0,0,exception);
3568 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
3569 cross_correlation=InverseFourierTransformImage(complex_multiplication,
3570 complex_multiplication->next,MagickFalse,exception);
3571 complex_multiplication=DestroyImageList(complex_multiplication);
3572 return(cross_correlation);
3575static MagickBooleanType SIMSetImageMean(Image *image,
3576 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3582 status = MagickTrue;
3590 image_view=AcquireAuthenticCacheView(image,exception);
3591#if defined(MAGICKCORE_OPENMP_SUPPORT)
3592 #pragma omp parallel for schedule(static) shared(status) \
3593 magick_number_threads(image,image,image->rows,1)
3595 for (y=0; y < (ssize_t) image->rows; y++)
3603 if (status == MagickFalse)
3605 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3606 if (q == (Quantum *) NULL)
3611 for (x=0; x < (ssize_t) image->columns; x++)
3616 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3618 PixelChannel channel = GetPixelChannelChannel(image,i);
3619 PixelTrait traits = GetPixelChannelTraits(image,channel);
3620 if ((traits & UpdatePixelTrait) == 0)
3622 q[i]=(Quantum) channel_statistics[channel].mean;
3624 q+=(ptrdiff_t) GetPixelChannels(image);
3626 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3629 image_view=DestroyCacheView(image_view);
3633static Image *SIMSubtractImageMean(
const Image *alpha_image,
3634 const Image *beta_image,
const ChannelStatistics *channel_statistics,
3635 ExceptionInfo *exception)
3645 status = MagickTrue;
3653 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
3654 MagickTrue,exception);
3655 if (subtract_image == (Image *) NULL)
3656 return(subtract_image);
3657 image_view=AcquireAuthenticCacheView(subtract_image,exception);
3658 beta_view=AcquireVirtualCacheView(beta_image,exception);
3659#if defined(MAGICKCORE_OPENMP_SUPPORT)
3660 #pragma omp parallel for schedule(static) shared(status) \
3661 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
3663 for (y=0; y < (ssize_t) subtract_image->rows; y++)
3674 if (status == MagickFalse)
3676 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
3677 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
3679 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3684 for (x=0; x < (ssize_t) subtract_image->columns; x++)
3689 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
3691 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
3692 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
3693 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3694 if (((traits & UpdatePixelTrait) == 0) ||
3695 ((beta_traits & UpdatePixelTrait) == 0))
3697 if ((x >= (ssize_t) beta_image->columns) ||
3698 (y >= (ssize_t) beta_image->rows))
3701 q[i]=(Quantum) ((
double) GetPixelChannel(beta_image,channel,p)-
3702 channel_statistics[channel].mean);
3704 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3705 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
3707 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3710 beta_view=DestroyCacheView(beta_view);
3711 image_view=DestroyCacheView(image_view);
3712 if (status == MagickFalse)
3713 subtract_image=DestroyImage(subtract_image);
3714 return(subtract_image);
3717static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
3718 ExceptionInfo *exception)
3727 status = MagickTrue;
3735 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
3736 MagickTrue,exception);
3737 if (unity_image == (Image *) NULL)
3738 return(unity_image);
3739 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
3740 return(DestroyImage(unity_image));
3741 image_view=AcquireAuthenticCacheView(unity_image,exception);
3742#if defined(MAGICKCORE_OPENMP_SUPPORT)
3743 #pragma omp parallel for schedule(static) shared(status) \
3744 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
3746 for (y=0; y < (ssize_t) unity_image->rows; y++)
3754 if (status == MagickFalse)
3756 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
3758 if (q == (Quantum *) NULL)
3763 for (x=0; x < (ssize_t) unity_image->columns; x++)
3768 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
3770 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
3771 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
3772 if ((traits & UpdatePixelTrait) == 0)
3774 if ((x >= (ssize_t) beta_image->columns) ||
3775 (y >= (ssize_t) beta_image->rows))
3780 q+=(ptrdiff_t) GetPixelChannels(unity_image);
3782 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3785 image_view=DestroyCacheView(image_view);
3786 if (status == MagickFalse)
3787 unity_image=DestroyImage(unity_image);
3788 return(unity_image);
3791static Image *SIMVarianceImage(Image *alpha_image,
const Image *beta_image,
3792 ExceptionInfo *exception)
3802 status = MagickTrue;
3810 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
3811 if (variance_image == (Image *) NULL)
3812 return(variance_image);
3813 image_view=AcquireAuthenticCacheView(variance_image,exception);
3814 beta_view=AcquireVirtualCacheView(beta_image,exception);
3815#if defined(MAGICKCORE_OPENMP_SUPPORT)
3816 #pragma omp parallel for schedule(static) shared(status) \
3817 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3819 for (y=0; y < (ssize_t) variance_image->rows; y++)
3830 if (status == MagickFalse)
3832 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3834 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3836 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3841 for (x=0; x < (ssize_t) variance_image->columns; x++)
3846 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3851 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3852 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3853 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3854 if (((traits & UpdatePixelTrait) == 0) ||
3855 ((beta_traits & UpdatePixelTrait) == 0))
3857 error=(double) q[i]-(
double) GetPixelChannel(beta_image,channel,p);
3858 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3859 (sqrt(fabs(QuantumScale*error))/sqrt((
double) QuantumRange))));
3861 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3862 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3864 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3867 beta_view=DestroyCacheView(beta_view);
3868 image_view=DestroyCacheView(image_view);
3869 if (status == MagickFalse)
3870 variance_image=DestroyImage(variance_image);
3871 return(variance_image);
3874static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3875 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
3877#define ThrowDPCSimilarityException() \
3879 if (dot_product_image != (Image *) NULL) \
3880 dot_product_image=DestroyImage(dot_product_image); \
3881 if (magnitude_image != (Image *) NULL) \
3882 magnitude_image=DestroyImage(magnitude_image); \
3883 if (reconstruct_image != (Image *) NULL) \
3884 reconstruct_image=DestroyImage(reconstruct_image); \
3885 if (rx_image != (Image *) NULL) \
3886 rx_image=DestroyImage(rx_image); \
3887 if (ry_image != (Image *) NULL) \
3888 ry_image=DestroyImage(ry_image); \
3889 if (target_image != (Image *) NULL) \
3890 target_image=DestroyImage(target_image); \
3891 if (threshold_image != (Image *) NULL) \
3892 threshold_image=DestroyImage(threshold_image); \
3893 if (trx_image != (Image *) NULL) \
3894 trx_image=DestroyImage(trx_image); \
3895 if (try_image != (Image *) NULL) \
3896 try_image=DestroyImage(try_image); \
3897 if (tx_image != (Image *) NULL) \
3898 tx_image=DestroyImage(tx_image); \
3899 if (ty_image != (Image *) NULL) \
3900 ty_image=DestroyImage(ty_image); \
3901 return((Image *) NULL); \
3908 standard_deviation = 0.0;
3911 *dot_product_image = (Image *) NULL,
3912 *magnitude_image = (Image *) NULL,
3913 *reconstruct_image = (Image *) NULL,
3914 *rx_image = (Image *) NULL,
3915 *ry_image = (Image *) NULL,
3916 *trx_image = (Image *) NULL,
3917 *target_image = (Image *) NULL,
3918 *threshold_image = (Image *) NULL,
3919 *try_image = (Image *) NULL,
3920 *tx_image = (Image *) NULL,
3921 *ty_image = (Image *) NULL;
3924 status = MagickTrue;
3932 target_image=CloneImage(image,0,0,MagickTrue,exception);
3933 if (target_image == (Image *) NULL)
3934 return((Image *) NULL);
3938 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3939 if (reconstruct_image == (Image *) NULL)
3940 ThrowDPCSimilarityException();
3944 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3946 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3947 if (rx_image == (Image *) NULL)
3948 ThrowDPCSimilarityException();
3949 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3950 reconstruct_image=DestroyImage(reconstruct_image);
3951 if (ry_image == (Image *) NULL)
3952 ThrowDPCSimilarityException();
3956 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3957 if (magnitude_image == (Image *) NULL)
3958 ThrowDPCSimilarityException();
3962 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3963 if (threshold_image == (Image *) NULL)
3964 ThrowDPCSimilarityException();
3965 status=BilevelImage(threshold_image,0.0,exception);
3966 if (status == MagickFalse)
3967 ThrowDPCSimilarityException();
3968 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3969 threshold_image=DestroyImage(threshold_image);
3970 if (status == MagickFalse)
3971 ThrowDPCSimilarityException();
3972 edge_factor=MagickSafeReciprocal(QuantumScale*mean*reconstruct->columns*
3973 reconstruct->rows)+QuantumScale;
3977 trx_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3978 rx_image=DestroyImage(rx_image);
3979 if (trx_image == (Image *) NULL)
3980 ThrowDPCSimilarityException();
3982 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3983 magnitude_image=DestroyImage(magnitude_image);
3984 ry_image=DestroyImage(ry_image);
3985 if (try_image == (Image *) NULL)
3986 ThrowDPCSimilarityException();
3991 (void) SetImageVirtualPixelMethod(target_image,EdgeVirtualPixelMethod,
3993 tx_image=SIMDerivativeImage(target_image,
"Sobel",exception);
3994 if (tx_image == (Image *) NULL)
3995 ThrowDPCSimilarityException();
3996 ty_image=SIMDerivativeImage(target_image,
"Sobel:90",exception);
3997 target_image=DestroyImage(target_image);
3998 if (ty_image == (Image *) NULL)
3999 ThrowDPCSimilarityException();
4003 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
4004 if (magnitude_image == (Image *) NULL)
4005 ThrowDPCSimilarityException();
4009 trx_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
4010 tx_image=DestroyImage(tx_image);
4011 if (trx_image == (Image *) NULL)
4012 ThrowDPCSimilarityException();
4014 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
4015 ty_image=DestroyImage(ty_image);
4016 magnitude_image=DestroyImage(magnitude_image);
4017 if (try_image == (Image *) NULL)
4018 ThrowDPCSimilarityException();
4023 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
4024 rx_image=DestroyImage(rx_image);
4025 tx_image=DestroyImage(tx_image);
4026 if (trx_image == (Image *) NULL)
4027 ThrowDPCSimilarityException();
4028 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
4029 ry_image=DestroyImage(ry_image);
4030 ty_image=DestroyImage(ty_image);
4031 if (try_image == (Image *) NULL)
4032 ThrowDPCSimilarityException();
4036 (void) SetImageArtifact(try_image,
"compose:clamp",
"false");
4037 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
4039 try_image=DestroyImage(try_image);
4040 if (status == MagickFalse)
4041 ThrowDPCSimilarityException();
4042 status=SIMMultiplyImage(trx_image,edge_factor,
4043 (
const ChannelStatistics *) NULL,exception);
4044 if (status == MagickFalse)
4045 ThrowDPCSimilarityException();
4049 SetGeometry(image,&geometry);
4050 geometry.width=image->columns;
4051 geometry.height=image->rows;
4052 (void) ResetImagePage(trx_image,
"0x0+0+0");
4053 dot_product_image=CropImage(trx_image,&geometry,exception);
4054 trx_image=DestroyImage(trx_image);
4055 if (dot_product_image == (Image *) NULL)
4056 ThrowDPCSimilarityException();
4057 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
4061 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
4063 if (status == MagickFalse)
4064 ThrowDPCSimilarityException();
4065 dot_product_image->depth=32;
4066 dot_product_image->colorspace=GRAYColorspace;
4067 dot_product_image->alpha_trait=UndefinedPixelTrait;
4068 status=SIMFilterImageNaNs(dot_product_image,exception);
4069 if (status == MagickFalse)
4070 ThrowDPCSimilarityException();
4071 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
4072 if (status == MagickFalse)
4073 ThrowDPCSimilarityException();
4074 if ((QuantumScale*maxima) > 1.0)
4076 status=SIMMultiplyImage(dot_product_image,1.0/(QuantumScale*maxima),
4077 (
const ChannelStatistics *) NULL,exception);
4078 maxima=(double) QuantumRange;
4080 *similarity_metric=QuantumScale*maxima;
4081 return(dot_product_image);
4084static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
4085 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4087#define ThrowMSESimilarityException() \
4089 if (alpha_image != (Image *) NULL) \
4090 alpha_image=DestroyImage(alpha_image); \
4091 if (beta_image != (Image *) NULL) \
4092 beta_image=DestroyImage(beta_image); \
4093 if (channel_statistics != (ChannelStatistics *) NULL) \
4094 channel_statistics=(ChannelStatistics *) \
4095 RelinquishMagickMemory(channel_statistics); \
4096 if (mean_image != (Image *) NULL) \
4097 mean_image=DestroyImage(mean_image); \
4098 if (mse_image != (Image *) NULL) \
4099 mse_image=DestroyImage(mse_image); \
4100 if (reconstruct_image != (Image *) NULL) \
4101 reconstruct_image=DestroyImage(reconstruct_image); \
4102 if (sum_image != (Image *) NULL) \
4103 sum_image=DestroyImage(sum_image); \
4104 if (alpha_image != (Image *) NULL) \
4105 alpha_image=DestroyImage(alpha_image); \
4106 return((Image *) NULL); \
4110 *channel_statistics = (ChannelStatistics *) NULL;
4116 *alpha_image = (Image *) NULL,
4117 *beta_image = (Image *) NULL,
4118 *mean_image = (Image *) NULL,
4119 *mse_image = (Image *) NULL,
4120 *reconstruct_image = (Image *) NULL,
4121 *sum_image = (Image *) NULL,
4122 *target_image = (Image *) NULL;
4125 status = MagickTrue;
4133 target_image=SIMSquareImage(image,exception);
4134 if (target_image == (Image *) NULL)
4135 ThrowMSESimilarityException();
4136 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4137 if (reconstruct_image == (Image *) NULL)
4138 ThrowMSESimilarityException();
4142 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4144 target_image=DestroyImage(target_image);
4145 if (alpha_image == (Image *) NULL)
4146 ThrowMSESimilarityException();
4147 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
4148 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
4149 if (status == MagickFalse)
4150 ThrowMSESimilarityException();
4154 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
4155 MagickTrue,0,0,exception);
4156 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4157 if (beta_image == (Image *) NULL)
4159 reconstruct_image=DestroyImage(reconstruct_image);
4160 ThrowMSESimilarityException();
4162 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
4163 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
4164 reconstruct_image=DestroyImage(reconstruct_image);
4165 if (status == MagickFalse)
4166 ThrowMSESimilarityException();
4170 sum_image=SIMSquareImage(reconstruct,exception);
4171 if (sum_image == (Image *) NULL)
4172 ThrowMSESimilarityException();
4173 channel_statistics=GetImageStatistics(sum_image,exception);
4174 if (channel_statistics == (ChannelStatistics *) NULL)
4175 ThrowMSESimilarityException();
4176 status=SetImageStorageClass(sum_image,DirectClass,exception);
4177 if (status == MagickFalse)
4178 ThrowMSESimilarityException();
4179 status=SIMSetImageMean(sum_image,channel_statistics,exception);
4180 channel_statistics=(ChannelStatistics *)
4181 RelinquishMagickMemory(channel_statistics);
4182 if (status == MagickFalse)
4183 ThrowMSESimilarityException();
4187 AppendImageToList(&sum_image,alpha_image);
4188 AppendImageToList(&sum_image,beta_image);
4189 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
4190 if (mean_image == (Image *) NULL)
4191 ThrowMSESimilarityException();
4192 sum_image=DestroyImage(sum_image);
4193 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
4194 if (status == MagickFalse)
4195 ThrowMSESimilarityException();
4199 SetGeometry(image,&geometry);
4200 geometry.width=image->columns;
4201 geometry.height=image->rows;
4202 (void) ResetImagePage(mean_image,
"0x0+0+0");
4203 mse_image=CropImage(mean_image,&geometry,exception);
4204 mean_image=DestroyImage(mean_image);
4205 if (mse_image == (Image *) NULL)
4206 ThrowMSESimilarityException();
4210 (void) ResetImagePage(mse_image,
"0x0+0+0");
4211 (void) ClampImage(mse_image,exception);
4212 mse_image->depth=32;
4213 mse_image->colorspace=GRAYColorspace;
4214 mse_image->alpha_trait=UndefinedPixelTrait;
4215 status=SIMMinimaImage(mse_image,&minima,offset,exception);
4216 if (status == MagickFalse)
4217 ThrowMSESimilarityException();
4218 status=NegateImage(mse_image,MagickFalse,exception);
4219 if (status == MagickFalse)
4220 ThrowMSESimilarityException();
4221 alpha_image=DestroyImage(alpha_image);
4222 beta_image=DestroyImage(beta_image);
4223 if ((QuantumScale*minima) < FLT_EPSILON)
4225 *similarity_metric=QuantumScale*minima;
4229static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
4230 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4232#define ThrowNCCSimilarityException() \
4234 if (alpha_image != (Image *) NULL) \
4235 alpha_image=DestroyImage(alpha_image); \
4236 if (beta_image != (Image *) NULL) \
4237 beta_image=DestroyImage(beta_image); \
4238 if (channel_statistics != (ChannelStatistics *) NULL) \
4239 channel_statistics=(ChannelStatistics *) \
4240 RelinquishMagickMemory(channel_statistics); \
4241 if (correlation_image != (Image *) NULL) \
4242 correlation_image=DestroyImage(correlation_image); \
4243 if (divide_image != (Image *) NULL) \
4244 divide_image=DestroyImage(divide_image); \
4245 if (ncc_image != (Image *) NULL) \
4246 ncc_image=DestroyImage(ncc_image); \
4247 if (normalize_image != (Image *) NULL) \
4248 normalize_image=DestroyImage(normalize_image); \
4249 if (reconstruct_image != (Image *) NULL) \
4250 reconstruct_image=DestroyImage(reconstruct_image); \
4251 if (target_image != (Image *) NULL) \
4252 target_image=DestroyImage(target_image); \
4253 if (variance_image != (Image *) NULL) \
4254 variance_image=DestroyImage(variance_image); \
4255 return((Image *) NULL); \
4259 *channel_statistics = (ChannelStatistics *) NULL;
4265 *alpha_image = (Image *) NULL,
4266 *beta_image = (Image *) NULL,
4267 *correlation_image = (Image *) NULL,
4268 *divide_image = (Image *) NULL,
4269 *ncc_image = (Image *) NULL,
4270 *normalize_image = (Image *) NULL,
4271 *reconstruct_image = (Image *) NULL,
4272 *target_image = (Image *) NULL,
4273 *variance_image = (Image *) NULL;
4276 status = MagickTrue;
4284 target_image=SIMSquareImage(image,exception);
4285 if (target_image == (Image *) NULL)
4286 ThrowNCCSimilarityException();
4287 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4288 if (reconstruct_image == (Image *) NULL)
4289 ThrowNCCSimilarityException();
4293 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4295 target_image=DestroyImage(target_image);
4296 if (alpha_image == (Image *) NULL)
4297 ThrowNCCSimilarityException();
4298 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
4299 reconstruct->columns*reconstruct->rows,(
const ChannelStatistics *) NULL,
4301 if (status == MagickFalse)
4302 ThrowNCCSimilarityException();
4306 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4307 reconstruct_image=DestroyImage(reconstruct_image);
4308 if (beta_image == (Image *) NULL)
4309 ThrowNCCSimilarityException();
4310 target_image=SIMSquareImage(beta_image,exception);
4311 beta_image=DestroyImage(beta_image);
4312 if (target_image == (Image *) NULL)
4313 ThrowNCCSimilarityException();
4314 status=SIMMultiplyImage(target_image,(
double) QuantumRange,
4315 (
const ChannelStatistics *) NULL,exception);
4316 if (status == MagickFalse)
4317 ThrowNCCSimilarityException();
4321 variance_image=SIMVarianceImage(alpha_image,target_image,exception);
4322 target_image=DestroyImage(target_image);
4323 alpha_image=DestroyImage(alpha_image);
4324 if (variance_image == (Image *) NULL)
4325 ThrowNCCSimilarityException();
4329 channel_statistics=GetImageStatistics(reconstruct,exception);
4330 if (channel_statistics == (ChannelStatistics *) NULL)
4331 ThrowNCCSimilarityException();
4332 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
4333 if (status == MagickFalse)
4334 ThrowNCCSimilarityException();
4335 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
4337 channel_statistics=(ChannelStatistics *)
4338 RelinquishMagickMemory(channel_statistics);
4339 if (normalize_image == (Image *) NULL)
4340 ThrowNCCSimilarityException();
4341 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
4342 normalize_image=DestroyImage(normalize_image);
4343 if (correlation_image == (Image *) NULL)
4344 ThrowNCCSimilarityException();
4348 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
4349 correlation_image=DestroyImage(correlation_image);
4350 variance_image=DestroyImage(variance_image);
4351 if (divide_image == (Image *) NULL)
4352 ThrowNCCSimilarityException();
4356 SetGeometry(image,&geometry);
4357 geometry.width=image->columns;
4358 geometry.height=image->rows;
4359 (void) ResetImagePage(divide_image,
"0x0+0+0");
4360 ncc_image=CropImage(divide_image,&geometry,exception);
4361 divide_image=DestroyImage(divide_image);
4362 if (ncc_image == (Image *) NULL)
4363 ThrowNCCSimilarityException();
4367 (void) ResetImagePage(ncc_image,
"0x0+0+0");
4368 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
4369 if (status == MagickFalse)
4370 ThrowNCCSimilarityException();
4371 ncc_image->depth=32;
4372 ncc_image->colorspace=GRAYColorspace;
4373 ncc_image->alpha_trait=UndefinedPixelTrait;
4374 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
4375 if (status == MagickFalse)
4376 ThrowNCCSimilarityException();
4377 if ((QuantumScale*maxima) > 1.0)
4379 status=SIMMultiplyImage(ncc_image,1.0/(QuantumScale*maxima),
4380 (
const ChannelStatistics *) NULL,exception);
4381 maxima=(double) QuantumRange;
4383 *similarity_metric=QuantumScale*maxima;
4387static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
4388 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4390#define ThrowPhaseSimilarityException() \
4392 if (phase_image != (Image *) NULL) \
4393 phase_image=DestroyImage(phase_image); \
4394 if (gamma_image != (Image *) NULL) \
4395 gamma_image=DestroyImage(gamma_image); \
4396 if (test_magnitude != (Image *) NULL) \
4397 test_magnitude=DestroyImage(test_magnitude); \
4398 if (magnitude_image != (Image *) NULL) \
4399 magnitude_image=DestroyImage(magnitude_image); \
4400 if (reconstruct_magnitude != (Image *) NULL) \
4401 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
4402 if (correlation_image != (Image *) NULL) \
4403 correlation_image=DestroyImage(correlation_image); \
4404 if (fft_images != (Image *) NULL) \
4405 fft_images=DestroyImageList(fft_images); \
4406 if (reconstruct_image != (Image *) NULL) \
4407 reconstruct_image=DestroyImage(reconstruct_image); \
4408 if (target_image != (Image *) NULL) \
4409 target_image=DestroyImage(target_image); \
4410 return((Image *) NULL); \
4417 *correlation_image = (Image *) NULL,
4418 *fft_images = (Image *) NULL,
4419 *gamma_image = (Image *) NULL,
4420 *magnitude_image = (Image *) NULL,
4421 *phase_image = (Image *) NULL,
4422 *reconstruct_image = (Image *) NULL,
4423 *reconstruct_magnitude = (Image *) NULL,
4424 *target_image = (Image *) NULL,
4425 *test_magnitude = (Image *) NULL;
4428 status = MagickTrue;
4436 target_image=CloneImage(image,0,0,MagickTrue,exception);
4437 if (target_image == (Image *) NULL)
4438 ThrowPhaseSimilarityException();
4439 (void) ResetImagePage(target_image,
"0x0+0+0");
4440 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4441 &target_image->background_color);
4442 status=SetImageExtent(target_image,2*CastDoubleToSizeT(ceil((
double)
4443 image->columns/2.0)),2*CastDoubleToSizeT(ceil((
double) image->rows/2.0)),
4445 if (status == MagickFalse)
4446 ThrowPhaseSimilarityException();
4450 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
4451 if (reconstruct_image == (Image *) NULL)
4452 ThrowPhaseSimilarityException();
4453 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
4454 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4455 &reconstruct_image->background_color);
4456 status=SetImageExtent(reconstruct_image,2*CastDoubleToSizeT(ceil((
double)
4457 image->columns/2.0)),2*CastDoubleToSizeT(ceil((
double) image->rows/2.0)),
4459 if (status == MagickFalse)
4460 ThrowPhaseSimilarityException();
4464 (void) SetImageArtifact(target_image,
"fourier:normalize",
"inverse");
4465 fft_images=ForwardFourierTransformImage(target_image,MagickTrue,exception);
4466 if (fft_images == (Image *) NULL)
4467 ThrowPhaseSimilarityException();
4468 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4469 fft_images=DestroyImageList(fft_images);
4470 if (test_magnitude == (Image *) NULL)
4471 ThrowPhaseSimilarityException();
4472 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
4473 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
4475 if (fft_images == (Image *) NULL)
4476 ThrowPhaseSimilarityException();
4477 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4478 fft_images=DestroyImageList(fft_images);
4479 if (reconstruct_magnitude == (Image *) NULL)
4480 ThrowPhaseSimilarityException();
4481 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
4482 if (magnitude_image == (Image *) NULL)
4483 ThrowPhaseSimilarityException();
4484 DisableCompositeClampUnlessSpecified(magnitude_image);
4485 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
4486 MagickTrue,0,0,exception);
4490 correlation_image=SIMPhaseCorrelationImage(target_image,reconstruct_image,
4491 magnitude_image,exception);
4492 target_image=DestroyImage(target_image);
4493 reconstruct_image=DestroyImage(reconstruct_image);
4494 test_magnitude=DestroyImage(test_magnitude);
4495 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
4496 if (correlation_image == (Image *) NULL)
4497 ThrowPhaseSimilarityException();
4498 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
4499 correlation_image=DestroyImage(correlation_image);
4500 if (gamma_image == (Image *) NULL)
4501 ThrowPhaseSimilarityException();
4505 SetGeometry(image,&geometry);
4506 geometry.width=image->columns;
4507 geometry.height=image->rows;
4508 (void) ResetImagePage(gamma_image,
"0x0+0+0");
4509 phase_image=CropImage(gamma_image,&geometry,exception);
4510 gamma_image=DestroyImage(gamma_image);
4511 if (phase_image == (Image *) NULL)
4512 ThrowPhaseSimilarityException();
4513 (void) ResetImagePage(phase_image,
"0x0+0+0");
4517 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
4518 if (status == MagickFalse)
4519 ThrowPhaseSimilarityException();
4520 phase_image->depth=32;
4521 phase_image->colorspace=GRAYColorspace;
4522 phase_image->alpha_trait=UndefinedPixelTrait;
4523 status=SIMFilterImageNaNs(phase_image,exception);
4524 if (status == MagickFalse)
4525 ThrowPhaseSimilarityException();
4526 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
4527 if (status == MagickFalse)
4528 ThrowPhaseSimilarityException();
4529 magnitude_image=DestroyImage(magnitude_image);
4530 *similarity_metric=QuantumScale*maxima;
4531 return(phase_image);
4534static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
4535 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4538 *psnr_image = (Image *) NULL;
4540 psnr_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4542 if (psnr_image == (Image *) NULL)
4544 *similarity_metric=10.0*MagickSafeLog10(MagickSafeReciprocal(
4545 *similarity_metric))/MagickSafePSNRRecipicol(10.0);
4549static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
4550 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4553 *rmse_image = (Image *) NULL;
4555 rmse_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4557 if (rmse_image == (Image *) NULL)
4559 *similarity_metric=sqrt(*similarity_metric);
4564static double GetSimilarityMetric(
const Image *image,
4565 const Image *reconstruct_image,
const MetricType metric,
4566 const ssize_t x_offset,
const ssize_t y_offset,ExceptionInfo *exception)
4569 *channel_similarity,
4573 *sans_exception = AcquireExceptionInfo();
4579 status = MagickTrue;
4585 length = MaxPixelChannels+1UL;
4587 SetGeometry(reconstruct_image,&geometry);
4588 geometry.x=x_offset;
4589 geometry.y=y_offset;
4590 similarity_image=CropImage(image,&geometry,sans_exception);
4591 sans_exception=DestroyExceptionInfo(sans_exception);
4592 if (similarity_image == (Image *) NULL)
4597 channel_similarity=(
double *) AcquireQuantumMemory(length,
4598 sizeof(*channel_similarity));
4599 if (channel_similarity == (
double *) NULL)
4601 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
4604 case AbsoluteErrorMetric:
4606 status=GetAESimilarity(similarity_image,reconstruct_image,
4607 channel_similarity,exception);
4610 case DotProductCorrelationErrorMetric:
4612 status=GetDPCSimilarity(similarity_image,reconstruct_image,
4613 channel_similarity,exception);
4616 case FuzzErrorMetric:
4618 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
4619 channel_similarity,exception);
4622 case MeanAbsoluteErrorMetric:
4624 status=GetMAESimilarity(similarity_image,reconstruct_image,
4625 channel_similarity,exception);
4628 case MeanErrorPerPixelErrorMetric:
4630 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
4631 channel_similarity,exception);
4634 case MeanSquaredErrorMetric:
4636 status=GetMSESimilarity(similarity_image,reconstruct_image,
4637 channel_similarity,exception);
4640 case NormalizedCrossCorrelationErrorMetric:
4642 status=GetNCCSimilarity(similarity_image,reconstruct_image,
4643 channel_similarity,exception);
4646 case PeakAbsoluteErrorMetric:
4648 status=GetPASimilarity(similarity_image,reconstruct_image,
4649 channel_similarity,exception);
4652 case PeakSignalToNoiseRatioErrorMetric:
4654 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
4655 channel_similarity,exception);
4658 case PerceptualHashErrorMetric:
4660 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
4661 channel_similarity,exception);
4664 case PhaseCorrelationErrorMetric:
4666 status=GetPHASESimilarity(similarity_image,reconstruct_image,
4667 channel_similarity,exception);
4670 case PixelDifferenceCountErrorMetric:
4672 status=GetPDCSimilarity(similarity_image,reconstruct_image,
4673 channel_similarity,exception);
4676 case RootMeanSquaredErrorMetric:
4677 case UndefinedErrorMetric:
4680 status=GetRMSESimilarity(similarity_image,reconstruct_image,
4681 channel_similarity,exception);
4684 case StructuralDissimilarityErrorMetric:
4686 status=GetDSSIMSimilarity(similarity_image,reconstruct_image,
4687 channel_similarity,exception);
4690 case StructuralSimilarityErrorMetric:
4692 status=GetSSIMSimularity(similarity_image,reconstruct_image,
4693 channel_similarity,exception);
4697 similarity_image=DestroyImage(similarity_image);
4698 similarity=channel_similarity[CompositePixelChannel];
4699 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
4700 if (status == MagickFalse)
4705MagickExport Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4706 const MetricType metric,
const double similarity_threshold,
4707 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4709#define SimilarityImageTag "Similarity/Image"
4725 *similarity_image = (Image *) NULL;
4728 status = MagickTrue;
4734 similarity_info = { 0.0, 0, 0 };
4743 assert(image != (
const Image *) NULL);
4744 assert(image->signature == MagickCoreSignature);
4745 assert(exception != (ExceptionInfo *) NULL);
4746 assert(exception->signature == MagickCoreSignature);
4747 assert(offset != (RectangleInfo *) NULL);
4748 if (IsEventLogging() != MagickFalse)
4749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4750 SetGeometry(reconstruct,offset);
4751 *similarity_metric=0.0;
4754#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4756 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4757 if (artifact == (
const char *) NULL)
4758 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4759 if (((artifact == (
const char *) NULL) ||
4760 (IsStringTrue(artifact) != MagickFalse)) &&
4761 ((image->channels & ReadMaskChannel) == 0))
4764 case DotProductCorrelationErrorMetric:
4766 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4767 similarity_metric,exception);
4768 return(similarity_image);
4770 case MeanSquaredErrorMetric:
4772 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4773 similarity_metric,exception);
4774 return(similarity_image);
4776 case NormalizedCrossCorrelationErrorMetric:
4778 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4779 similarity_metric,exception);
4780 return(similarity_image);
4782 case PeakSignalToNoiseRatioErrorMetric:
4784 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4785 similarity_metric,exception);
4786 return(similarity_image);
4788 case PhaseCorrelationErrorMetric:
4790 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4791 similarity_metric,exception);
4792 return(similarity_image);
4794 case RootMeanSquaredErrorMetric:
4795 case UndefinedErrorMetric:
4797 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4798 similarity_metric,exception);
4799 return(similarity_image);
4806 if ((image->columns < reconstruct->columns) ||
4807 (image->rows < reconstruct->rows))
4809 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4810 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4811 return((Image *) NULL);
4813 SetImageCompareBounds(image,reconstruct,&columns,&rows);
4814 similarity_image=CloneImage(image,columns,rows,MagickTrue,exception);
4815 if (similarity_image == (Image *) NULL)
4816 return((Image *) NULL);
4817 similarity_image->depth=32;
4818 similarity_image->colorspace=GRAYColorspace;
4819 similarity_image->alpha_trait=UndefinedPixelTrait;
4820 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4821 if (status == MagickFalse)
4822 return(DestroyImage(similarity_image));
4826 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
4827 similarity_info.x,similarity_info.y,exception);
4828 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4829#if defined(MAGICKCORE_OPENMP_SUPPORT)
4830 #pragma omp parallel for schedule(static) shared(similarity_info,status) \
4831 magick_number_threads(image,reconstruct,similarity_image->rows,1)
4833 for (y=0; y < (ssize_t) similarity_image->rows; y++)
4839 threshold_trigger = MagickFalse;
4845 channel_info = similarity_info;
4850 if (status == MagickFalse)
4852 if (threshold_trigger != MagickFalse)
4854 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4855 similarity_image->columns,1,exception);
4856 if (q == (Quantum *) NULL)
4861 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4866 similarity=GetSimilarityMetric((Image *) image,reconstruct,metric,x,y,
4870 case DotProductCorrelationErrorMetric:
4871 case NormalizedCrossCorrelationErrorMetric:
4872 case PeakSignalToNoiseRatioErrorMetric:
4873 case PhaseCorrelationErrorMetric:
4874 case StructuralSimilarityErrorMetric:
4876 if (similarity <= channel_info.similarity)
4878 channel_info.similarity=similarity;
4885 if (similarity >= channel_info.similarity)
4887 channel_info.similarity=similarity;
4893 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4895 PixelChannel channel = GetPixelChannelChannel(image,i);
4896 PixelTrait traits = GetPixelChannelTraits(image,channel);
4897 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4899 if (((traits & UpdatePixelTrait) == 0) ||
4900 ((similarity_traits & UpdatePixelTrait) == 0))
4904 case DotProductCorrelationErrorMetric:
4905 case NormalizedCrossCorrelationErrorMetric:
4906 case PeakSignalToNoiseRatioErrorMetric:
4907 case PhaseCorrelationErrorMetric:
4908 case StructuralSimilarityErrorMetric:
4910 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4911 QuantumRange*similarity),q);
4916 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4917 QuantumRange*(1.0-similarity)),q);
4922 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4924#if defined(MAGICKCORE_OPENMP_SUPPORT)
4925 #pragma omp critical (MagickCore_GetSimilarityMetric)
4929 case DotProductCorrelationErrorMetric:
4930 case NormalizedCrossCorrelationErrorMetric:
4931 case PeakSignalToNoiseRatioErrorMetric:
4932 case PhaseCorrelationErrorMetric:
4933 case StructuralSimilarityErrorMetric:
4935 if (similarity_threshold != DefaultSimilarityThreshold)
4936 if (channel_info.similarity >= similarity_threshold)
4937 threshold_trigger=MagickTrue;
4938 if (channel_info.similarity >= similarity_info.similarity)
4939 similarity_info=channel_info;
4944 if (similarity_threshold != DefaultSimilarityThreshold)
4945 if (channel_info.similarity < similarity_threshold)
4946 threshold_trigger=MagickTrue;
4947 if (channel_info.similarity < similarity_info.similarity)
4948 similarity_info=channel_info;
4952 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4954 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4960 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4961 if (proceed == MagickFalse)
4965 similarity_view=DestroyCacheView(similarity_view);
4966 if (status == MagickFalse)
4967 similarity_image=DestroyImage(similarity_image);
4968 *similarity_metric=similarity_info.similarity;
4969 if (fabs(*similarity_metric) < MagickEpsilon)
4970 *similarity_metric=0.0;
4971 offset->x=similarity_info.x;
4972 offset->y=similarity_info.y;
4973 (void) FormatImageProperty((Image *) image,
"similarity",
"%.*g",
4974 GetMagickPrecision(),*similarity_metric);
4975 (void) FormatImageProperty((Image *) image,
"similarity.offset.x",
"%.*g",
4976 GetMagickPrecision(),(
double) offset->x);
4977 (void) FormatImageProperty((Image *) image,
"similarity.offset.y",
"%.*g",
4978 GetMagickPrecision(),(
double) offset->y);
4979 return(similarity_image);