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)
332 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
333 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
334 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
335 image_view=AcquireVirtualCacheView(image,exception);
336 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
337#if defined(MAGICKCORE_OPENMP_SUPPORT)
338 #pragma omp parallel for schedule(static) shared(similarity,status) \
339 magick_number_threads(image,image,rows,1)
341 for (y=0; y < (ssize_t) rows; y++)
348 channel_similarity[MaxPixelChannels+1] = { 0.0 };
353 if (status == MagickFalse)
355 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
356 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
357 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
362 for (x=0; x < (ssize_t) columns; x++)
374 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
375 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
377 p+=(ptrdiff_t) GetPixelChannels(image);
378 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
381 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
382 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
383 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
388 PixelChannel channel = GetPixelChannelChannel(image,i);
389 PixelTrait traits = GetPixelChannelTraits(image,channel);
390 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
392 if (((traits & UpdatePixelTrait) == 0) ||
393 ((reconstruct_traits & UpdatePixelTrait) == 0))
395 if (channel == AlphaPixelChannel)
396 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
399 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
400 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
402 channel_similarity[i]++;
407 channel_similarity[CompositePixelChannel]++;
408 p+=(ptrdiff_t) GetPixelChannels(image);
409 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
411#if defined(MAGICKCORE_OPENMP_SUPPORT)
412 #pragma omp critical (MagickCore_GetAESimilarity)
418 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
420 PixelChannel channel = GetPixelChannelChannel(image,j);
421 PixelTrait traits = GetPixelChannelTraits(image,channel);
422 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
424 if (((traits & UpdatePixelTrait) == 0) ||
425 ((reconstruct_traits & UpdatePixelTrait) == 0))
427 similarity[j]+=channel_similarity[j];
429 similarity[CompositePixelChannel]+=
430 channel_similarity[CompositePixelChannel];
433 reconstruct_view=DestroyCacheView(reconstruct_view);
434 image_view=DestroyCacheView(image_view);
435 area=MagickSafeReciprocal((
double) columns*rows);
436 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
438 similarity[CompositePixelChannel]*=area;
442static MagickBooleanType GetDPCSimilarity(
const Image *image,
443 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
445#define SimilarityImageTag "Similarity/Image"
453 *reconstruct_statistics;
456 norm[MaxPixelChannels+1] = { 0.0 },
457 reconstruct_norm[MaxPixelChannels+1] = { 0.0 };
476 image_statistics=GetImageStatistics(image,exception);
477 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
478 if ((image_statistics == (ChannelStatistics *) NULL) ||
479 (reconstruct_statistics == (ChannelStatistics *) NULL))
481 if (image_statistics != (ChannelStatistics *) NULL)
482 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
484 if (reconstruct_statistics != (ChannelStatistics *) NULL)
485 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
486 reconstruct_statistics);
489 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
490 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
491 image_view=AcquireVirtualCacheView(image,exception);
492 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
493#if defined(MAGICKCORE_OPENMP_SUPPORT)
494 #pragma omp parallel for schedule(static) shared(norm,reconstruct_norm,similarity,status) \
495 magick_number_threads(image,image,rows,1)
497 for (y=0; y < (ssize_t) rows; y++)
504 channel_norm[MaxPixelChannels+1] = { 0.0 },
505 channel_reconstruct_norm[MaxPixelChannels+1] = { 0.0 },
506 channel_similarity[MaxPixelChannels+1] = { 0.0 };
511 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
512 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
513 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
518 for (x=0; x < (ssize_t) columns; x++)
527 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
528 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
530 p+=(ptrdiff_t) GetPixelChannels(image);
531 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
534 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
535 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
536 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
542 PixelChannel channel = GetPixelChannelChannel(image,i);
543 PixelTrait traits = GetPixelChannelTraits(image,channel);
544 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
546 if (((traits & UpdatePixelTrait) == 0) ||
547 ((reconstruct_traits & UpdatePixelTrait) == 0))
549 if (channel == AlphaPixelChannel)
551 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
552 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
553 channel,q)-reconstruct_statistics[channel].mean);
557 alpha=QuantumScale*(Sa*p[i]-image_statistics[channel].mean);
558 beta=QuantumScale*(Da*GetPixelChannel(reconstruct_image,channel,
559 q)-reconstruct_statistics[channel].mean);
561 channel_similarity[i]+=alpha*beta;
562 channel_norm[i]+=alpha*alpha;
563 channel_reconstruct_norm[i]+=beta*beta;
565 p+=(ptrdiff_t) GetPixelChannels(image);
566 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
568#if defined(MAGICKCORE_OPENMP_SUPPORT)
569 #pragma omp critical (MagickCore_GetDPCSimilarity)
575 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
577 PixelChannel channel = GetPixelChannelChannel(image,j);
578 PixelTrait traits = GetPixelChannelTraits(image,channel);
579 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
581 if (((traits & UpdatePixelTrait) == 0) ||
582 ((reconstruct_traits & UpdatePixelTrait) == 0))
584 similarity[j]+=channel_similarity[j];
585 similarity[CompositePixelChannel]+=channel_similarity[j];
586 norm[j]+=channel_norm[j];
587 norm[CompositePixelChannel]+=channel_norm[j];
588 reconstruct_norm[j]+=channel_reconstruct_norm[j];
589 reconstruct_norm[CompositePixelChannel]+=channel_reconstruct_norm[j];
592 if (image->progress_monitor != (MagickProgressMonitor) NULL)
597#if defined(MAGICKCORE_OPENMP_SUPPORT)
601 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
602 if (proceed == MagickFalse)
609 reconstruct_view=DestroyCacheView(reconstruct_view);
610 image_view=DestroyCacheView(image_view);
614 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
616 PixelChannel channel = GetPixelChannelChannel(image,k);
617 PixelTrait traits = GetPixelChannelTraits(image,channel);
618 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
620 if (((traits & UpdatePixelTrait) == 0) ||
621 ((reconstruct_traits & UpdatePixelTrait) == 0))
623 similarity[k]*=MagickSafeReciprocal(sqrt(norm[k]*reconstruct_norm[k]));
625 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
626 norm[CompositePixelChannel]*reconstruct_norm[CompositePixelChannel]));
630 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
631 reconstruct_statistics);
632 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
637static MagickBooleanType GetFUZZSimilarity(
const Image *image,
638 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
662 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
663 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
664 image_view=AcquireVirtualCacheView(image,exception);
665 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
666#if defined(MAGICKCORE_OPENMP_SUPPORT)
667 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
668 magick_number_threads(image,image,rows,1)
670 for (y=0; y < (ssize_t) rows; y++)
678 channel_similarity[MaxPixelChannels+1] = { 0.0 };
683 if (status == MagickFalse)
685 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
686 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
687 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
692 for (x=0; x < (ssize_t) columns; x++)
701 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
702 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
704 p+=(ptrdiff_t) GetPixelChannels(image);
705 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
708 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
709 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
710 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
715 PixelChannel channel = GetPixelChannelChannel(image,i);
716 PixelTrait traits = GetPixelChannelTraits(image,channel);
717 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
719 if (((traits & UpdatePixelTrait) == 0) ||
720 ((reconstruct_traits & UpdatePixelTrait) == 0))
722 if (channel == AlphaPixelChannel)
723 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
726 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
727 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
729 channel_similarity[i]+=QuantumScale*error*QuantumScale*error;
730 channel_similarity[CompositePixelChannel]+=QuantumScale*error*
735 p+=(ptrdiff_t) GetPixelChannels(image);
736 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
738#if defined(MAGICKCORE_OPENMP_SUPPORT)
739 #pragma omp critical (MagickCore_GetFUZZSimilarity)
746 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
748 PixelChannel channel = GetPixelChannelChannel(image,j);
749 PixelTrait traits = GetPixelChannelTraits(image,channel);
750 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
752 if (((traits & UpdatePixelTrait) == 0) ||
753 ((reconstruct_traits & UpdatePixelTrait) == 0))
755 similarity[j]+=channel_similarity[j];
757 similarity[CompositePixelChannel]+=
758 channel_similarity[CompositePixelChannel];
761 reconstruct_view=DestroyCacheView(reconstruct_view);
762 image_view=DestroyCacheView(image_view);
763 area=MagickSafeReciprocal(area);
764 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
766 PixelChannel channel = GetPixelChannelChannel(image,k);
767 PixelTrait traits = GetPixelChannelTraits(image,channel);
768 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
770 if (((traits & UpdatePixelTrait) == 0) ||
771 ((reconstruct_traits & UpdatePixelTrait) == 0))
775 similarity[CompositePixelChannel]*=area;
779static MagickBooleanType GetMAESimilarity(
const Image *image,
780 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
803 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
804 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
805 image_view=AcquireVirtualCacheView(image,exception);
806 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
807#if defined(MAGICKCORE_OPENMP_SUPPORT)
808 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
809 magick_number_threads(image,image,rows,1)
811 for (y=0; y < (ssize_t) rows; y++)
819 channel_similarity[MaxPixelChannels+1] = { 0.0 };
824 if (status == MagickFalse)
826 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
827 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
828 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
833 for (x=0; x < (ssize_t) columns; x++)
842 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
843 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
845 p+=(ptrdiff_t) GetPixelChannels(image);
846 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
849 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
850 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
851 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
856 PixelChannel channel = GetPixelChannelChannel(image,i);
857 PixelTrait traits = GetPixelChannelTraits(image,channel);
858 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
860 if (((traits & UpdatePixelTrait) == 0) ||
861 ((reconstruct_traits & UpdatePixelTrait) == 0))
863 if (channel == AlphaPixelChannel)
864 error=QuantumScale*fabs((
double) p[i]-(
double) GetPixelChannel(
865 reconstruct_image,channel,q));
867 error=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
869 channel_similarity[i]+=error;
870 channel_similarity[CompositePixelChannel]+=error;
873 p+=(ptrdiff_t) GetPixelChannels(image);
874 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
876#if defined(MAGICKCORE_OPENMP_SUPPORT)
877 #pragma omp critical (MagickCore_GetMAESimilarity)
884 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
886 PixelChannel channel = GetPixelChannelChannel(image,j);
887 PixelTrait traits = GetPixelChannelTraits(image,channel);
888 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
890 if (((traits & UpdatePixelTrait) == 0) ||
891 ((reconstruct_traits & UpdatePixelTrait) == 0))
893 similarity[j]+=channel_similarity[j];
895 similarity[CompositePixelChannel]+=
896 channel_similarity[CompositePixelChannel];
899 reconstruct_view=DestroyCacheView(reconstruct_view);
900 image_view=DestroyCacheView(image_view);
901 area=MagickSafeReciprocal(area);
902 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
904 PixelChannel channel = GetPixelChannelChannel(image,k);
905 PixelTrait traits = GetPixelChannelTraits(image,channel);
906 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
908 if (((traits & UpdatePixelTrait) == 0) ||
909 ((reconstruct_traits & UpdatePixelTrait) == 0))
913 similarity[CompositePixelChannel]*=area;
914 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
918static MagickBooleanType GetMEPPSimilarity(Image *image,
919 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
927 maximum_error = -MagickMaximumValue,
944 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
945 image_view=AcquireVirtualCacheView(image,exception);
946 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
947#if defined(MAGICKCORE_OPENMP_SUPPORT)
948 #pragma omp parallel for schedule(static) shared(area,similarity,maximum_error,mean_error,status) \
949 magick_number_threads(image,image,rows,1)
951 for (y=0; y < (ssize_t) rows; y++)
959 channel_similarity[MaxPixelChannels+1] = { 0.0 },
960 channel_maximum_error = maximum_error,
961 channel_mean_error = 0.0;
966 if (status == MagickFalse)
968 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
969 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
970 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
975 for (x=0; x < (ssize_t) columns; x++)
984 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
985 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
987 p+=(ptrdiff_t) GetPixelChannels(image);
988 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
991 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
992 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
993 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
998 PixelChannel channel = GetPixelChannelChannel(image,i);
999 PixelTrait traits = GetPixelChannelTraits(image,channel);
1000 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1002 if (((traits & UpdatePixelTrait) == 0) ||
1003 ((reconstruct_traits & UpdatePixelTrait) == 0))
1005 if (channel == AlphaPixelChannel)
1006 error=QuantumScale*fabs((
double) p[i]-(
double) GetPixelChannel(
1007 reconstruct_image,channel,q));
1009 error=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
1011 channel_similarity[i]+=error;
1012 channel_similarity[CompositePixelChannel]+=error;
1013 channel_mean_error+=error*error;
1014 if (error > channel_maximum_error)
1015 channel_maximum_error=error;
1018 p+=(ptrdiff_t) GetPixelChannels(image);
1019 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1021#if defined(MAGICKCORE_OPENMP_SUPPORT)
1022 #pragma omp critical (MagickCore_GetMEPPSimilarity)
1029 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1031 PixelChannel channel = GetPixelChannelChannel(image,j);
1032 PixelTrait traits = GetPixelChannelTraits(image,channel);
1033 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1035 if (((traits & UpdatePixelTrait) == 0) ||
1036 ((reconstruct_traits & UpdatePixelTrait) == 0))
1038 similarity[j]+=channel_similarity[j];
1040 similarity[CompositePixelChannel]+=
1041 channel_similarity[CompositePixelChannel];
1042 mean_error+=channel_mean_error;
1043 if (channel_maximum_error > maximum_error)
1044 maximum_error=channel_maximum_error;
1047 reconstruct_view=DestroyCacheView(reconstruct_view);
1048 image_view=DestroyCacheView(image_view);
1049 area=MagickSafeReciprocal(area);
1050 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1052 PixelChannel channel = GetPixelChannelChannel(image,k);
1053 PixelTrait traits = GetPixelChannelTraits(image,channel);
1054 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1056 if (((traits & UpdatePixelTrait) == 0) ||
1057 ((reconstruct_traits & UpdatePixelTrait) == 0))
1059 similarity[k]*=area;
1061 similarity[CompositePixelChannel]*=area;
1062 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1063 image->error.mean_error_per_pixel=QuantumRange*
1064 similarity[CompositePixelChannel];
1065 image->error.normalized_mean_error=mean_error*area;
1066 image->error.normalized_maximum_error=maximum_error;
1070static MagickBooleanType GetMSESimilarity(
const Image *image,
1071 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1081 status = MagickTrue;
1094 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1095 image_view=AcquireVirtualCacheView(image,exception);
1096 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1097#if defined(MAGICKCORE_OPENMP_SUPPORT)
1098 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1099 magick_number_threads(image,image,rows,1)
1101 for (y=0; y < (ssize_t) rows; y++)
1109 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1114 if (status == MagickFalse)
1116 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1117 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1118 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1123 for (x=0; x < (ssize_t) columns; x++)
1132 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1133 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1135 p+=(ptrdiff_t) GetPixelChannels(image);
1136 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1139 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1140 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1141 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1146 PixelChannel channel = GetPixelChannelChannel(image,i);
1147 PixelTrait traits = GetPixelChannelTraits(image,channel);
1148 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1150 if (((traits & UpdatePixelTrait) == 0) ||
1151 ((reconstruct_traits & UpdatePixelTrait) == 0))
1153 if (channel == AlphaPixelChannel)
1154 error=QuantumScale*((double) p[i]-(double) GetPixelChannel(
1155 reconstruct_image,channel,q));
1157 error=QuantumScale*(Sa*p[i]-Da*GetPixelChannel(reconstruct_image,
1159 channel_similarity[i]+=error*error;
1160 channel_similarity[CompositePixelChannel]+=error*error;
1163 p+=(ptrdiff_t) GetPixelChannels(image);
1164 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1166#if defined(MAGICKCORE_OPENMP_SUPPORT)
1167 #pragma omp critical (MagickCore_GetMSESimilarity)
1174 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1176 PixelChannel channel = GetPixelChannelChannel(image,j);
1177 PixelTrait traits = GetPixelChannelTraits(image,channel);
1178 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1180 if (((traits & UpdatePixelTrait) == 0) ||
1181 ((reconstruct_traits & UpdatePixelTrait) == 0))
1183 similarity[j]+=channel_similarity[j];
1185 similarity[CompositePixelChannel]+=
1186 channel_similarity[CompositePixelChannel];
1189 reconstruct_view=DestroyCacheView(reconstruct_view);
1190 image_view=DestroyCacheView(image_view);
1191 area=MagickSafeReciprocal(area);
1192 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1194 PixelChannel channel = GetPixelChannelChannel(image,k);
1195 PixelTrait traits = GetPixelChannelTraits(image,channel);
1196 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1198 if (((traits & UpdatePixelTrait) == 0) ||
1199 ((reconstruct_traits & UpdatePixelTrait) == 0))
1201 similarity[k]*=area;
1203 similarity[CompositePixelChannel]*=area;
1204 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1208static MagickBooleanType GetNCCSimilarity(
const Image *image,
1209 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1217 *reconstruct_statistics;
1220 reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1221 variance[MaxPixelChannels+1] = { 0.0 };
1224 status = MagickTrue;
1240 image_statistics=GetImageStatistics(image,exception);
1241 reconstruct_statistics=GetImageStatistics(reconstruct_image,exception);
1242 if ((image_statistics == (ChannelStatistics *) NULL) ||
1243 (reconstruct_statistics == (ChannelStatistics *) NULL))
1245 if (image_statistics != (ChannelStatistics *) NULL)
1246 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1248 if (reconstruct_statistics != (ChannelStatistics *) NULL)
1249 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1250 reconstruct_statistics);
1251 return(MagickFalse);
1253 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1254 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1255 image_view=AcquireVirtualCacheView(image,exception);
1256 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1257#if defined(MAGICKCORE_OPENMP_SUPPORT)
1258 #pragma omp parallel for schedule(static) shared(variance,reconstruct_variance,similarity,status) \
1259 magick_number_threads(image,image,rows,1)
1261 for (y=0; y < (ssize_t) rows; y++)
1268 channel_reconstruct_variance[MaxPixelChannels+1] = { 0.0 },
1269 channel_similarity[MaxPixelChannels+1] = { 0.0 },
1270 channel_variance[MaxPixelChannels+1] = { 0.0 };
1275 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1276 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1277 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1282 for (x=0; x < (ssize_t) columns; x++)
1291 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1292 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1294 p+=(ptrdiff_t) GetPixelChannels(image);
1295 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1298 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1299 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1300 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1306 PixelChannel channel = GetPixelChannelChannel(image,i);
1307 PixelTrait traits = GetPixelChannelTraits(image,channel);
1308 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1310 if (((traits & UpdatePixelTrait) == 0) ||
1311 ((reconstruct_traits & UpdatePixelTrait) == 0))
1313 if (channel == AlphaPixelChannel)
1315 alpha=QuantumScale*((double) p[i]-image_statistics[channel].mean);
1316 beta=QuantumScale*((double) GetPixelChannel(reconstruct_image,
1317 channel,q)-reconstruct_statistics[channel].mean);
1321 alpha=QuantumScale*(Sa*p[i]-image_statistics[channel].mean);
1322 beta=QuantumScale*(Da*GetPixelChannel(reconstruct_image,channel,
1323 q)-reconstruct_statistics[channel].mean);
1325 channel_similarity[i]+=alpha*beta;
1326 channel_variance[i]+=alpha*alpha;
1327 channel_reconstruct_variance[i]+=beta*beta;
1329 p+=(ptrdiff_t) GetPixelChannels(image);
1330 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1332#if defined(MAGICKCORE_OPENMP_SUPPORT)
1333 #pragma omp critical (MagickCore_GetNCCSimilarity)
1339 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1341 PixelChannel channel = GetPixelChannelChannel(image,j);
1342 PixelTrait traits = GetPixelChannelTraits(image,channel);
1343 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1345 if (((traits & UpdatePixelTrait) == 0) ||
1346 ((reconstruct_traits & UpdatePixelTrait) == 0))
1348 similarity[j]+=channel_similarity[j];
1349 similarity[CompositePixelChannel]+=channel_similarity[j];
1350 variance[j]+=channel_variance[j];
1351 variance[CompositePixelChannel]+=channel_variance[j];
1352 reconstruct_variance[j]+=channel_reconstruct_variance[j];
1353 reconstruct_variance[CompositePixelChannel]+=
1354 channel_reconstruct_variance[j];
1357 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1362#if defined(MAGICKCORE_OPENMP_SUPPORT)
1366 proceed=SetImageProgress(image,SimilarityImageTag,progress,rows);
1367 if (proceed == MagickFalse)
1374 reconstruct_view=DestroyCacheView(reconstruct_view);
1375 image_view=DestroyCacheView(image_view);
1379 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1381 PixelChannel channel = GetPixelChannelChannel(image,k);
1382 PixelTrait traits = GetPixelChannelTraits(image,channel);
1383 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1385 if (((traits & UpdatePixelTrait) == 0) ||
1386 ((reconstruct_traits & UpdatePixelTrait) == 0))
1388 similarity[k]*=MagickSafeReciprocal(sqrt(variance[k])*
1389 sqrt(reconstruct_variance[k]));
1391 similarity[CompositePixelChannel]*=MagickSafeReciprocal(sqrt(
1392 variance[CompositePixelChannel])*sqrt(
1393 reconstruct_variance[CompositePixelChannel]));
1397 reconstruct_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1398 reconstruct_statistics);
1399 image_statistics=(ChannelStatistics *) RelinquishMagickMemory(
1404static MagickBooleanType GetPASimilarity(
const Image *image,
1405 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1412 status = MagickTrue;
1424 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1425 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1426 image_view=AcquireVirtualCacheView(image,exception);
1427 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1428#if defined(MAGICKCORE_OPENMP_SUPPORT)
1429 #pragma omp parallel for schedule(static) shared(similarity,status) \
1430 magick_number_threads(image,image,rows,1)
1432 for (y=0; y < (ssize_t) rows; y++)
1439 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1444 if (status == MagickFalse)
1446 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1447 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1448 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1453 for (x=0; x < (ssize_t) columns; x++)
1462 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1463 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1465 p+=(ptrdiff_t) GetPixelChannels(image);
1466 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1469 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1470 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1471 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1476 PixelChannel channel = GetPixelChannelChannel(image,i);
1477 PixelTrait traits = GetPixelChannelTraits(image,channel);
1478 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1480 if (((traits & UpdatePixelTrait) == 0) ||
1481 ((reconstruct_traits & UpdatePixelTrait) == 0))
1483 if (channel == AlphaPixelChannel)
1484 distance=QuantumScale*fabs((
double) p[i]-(
double)
1485 GetPixelChannel(reconstruct_image,channel,q));
1487 distance=QuantumScale*fabs(Sa*p[i]-Da*GetPixelChannel(
1488 reconstruct_image,channel,q));
1489 if (distance > channel_similarity[i])
1490 channel_similarity[i]=distance;
1491 if (distance > channel_similarity[CompositePixelChannel])
1492 channel_similarity[CompositePixelChannel]=distance;
1494 p+=(ptrdiff_t) GetPixelChannels(image);
1495 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1497#if defined(MAGICKCORE_OPENMP_SUPPORT)
1498 #pragma omp critical (MagickCore_GetPASimilarity)
1504 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1506 PixelChannel channel = GetPixelChannelChannel(image,j);
1507 PixelTrait traits = GetPixelChannelTraits(image,channel);
1508 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1510 if (((traits & UpdatePixelTrait) == 0) ||
1511 ((reconstruct_traits & UpdatePixelTrait) == 0))
1513 if (channel_similarity[j] > similarity[j])
1514 similarity[j]=channel_similarity[j];
1516 if (channel_similarity[CompositePixelChannel] > similarity[CompositePixelChannel])
1517 similarity[CompositePixelChannel]=
1518 channel_similarity[CompositePixelChannel];
1521 reconstruct_view=DestroyCacheView(reconstruct_view);
1522 image_view=DestroyCacheView(image_view);
1526static MagickBooleanType GetPDCSimilarity(
const Image *image,
1527 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1538 status = MagickTrue;
1551 fuzz=GetFuzzyColorDistance(image,reconstruct_image);
1552 (void) memset(similarity,0,(MaxPixelChannels+1)*
sizeof(*similarity));
1553 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1554 image_view=AcquireVirtualCacheView(image,exception);
1555 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1556#if defined(MMAGICKCORE_OPENMP_SUPPORT)
1557 #pragma omp parallel for schedule(static) shared(similarity,status) \
1558 magick_number_threads(image,image,rows,1)
1560 for (y=0; y < (ssize_t) rows; y++)
1567 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1572 if (status == MagickFalse)
1574 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1575 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1576 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1581 for (x=0; x < (ssize_t) columns; x++)
1593 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1594 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1596 p+=(ptrdiff_t) GetPixelChannels(image);
1597 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1600 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1601 Da=QuantumScale*(double) GetPixelAlpha(reconstruct_image,q);
1602 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1607 PixelChannel channel = GetPixelChannelChannel(image,i);
1608 PixelTrait traits = GetPixelChannelTraits(image,channel);
1609 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1611 if (((traits & UpdatePixelTrait) == 0) ||
1612 ((reconstruct_traits & UpdatePixelTrait) == 0))
1614 if (channel == AlphaPixelChannel)
1615 error=(double) p[i]-(
double) GetPixelChannel(reconstruct_image,
1618 error=Sa*p[i]-Da*GetPixelChannel(reconstruct_image,channel,q);
1619 if (MagickSafeSignificantError(error*error,fuzz) != MagickFalse)
1621 channel_similarity[i]++;
1626 channel_similarity[CompositePixelChannel]++;
1627 p+=(ptrdiff_t) GetPixelChannels(image);
1628 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1630#if defined(MAGICKCORE_OPENMP_SUPPORT)
1631 #pragma omp critical (MagickCore_GetPDCSimilarity)
1637 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1639 PixelChannel channel = GetPixelChannelChannel(image,j);
1640 PixelTrait traits = GetPixelChannelTraits(image,channel);
1641 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1643 if (((traits & UpdatePixelTrait) == 0) ||
1644 ((reconstruct_traits & UpdatePixelTrait) == 0))
1646 similarity[j]+=channel_similarity[j];
1648 similarity[CompositePixelChannel]+=
1649 channel_similarity[CompositePixelChannel];
1652 reconstruct_view=DestroyCacheView(reconstruct_view);
1653 image_view=DestroyCacheView(image_view);
1654 area=MagickSafeReciprocal((
double) columns*rows);
1655 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1656 similarity[k]*=area;
1657 similarity[CompositePixelChannel]*=area;
1661static MagickBooleanType DFTPhaseSpectrum(
const Image *image,
const ssize_t u,
1662 const ssize_t v,
double *phase,ExceptionInfo *exception)
1664#define PhaseImageTag "Phase/Image"
1670 channel_imag[MaxPixelChannels+1] = { 0.0 },
1671 channel_real[MaxPixelChannels+1] = { 0.0 };
1684 image_view=AcquireVirtualCacheView(image,exception);
1685 for (y=0; y < (ssize_t) image->rows; y++)
1693 if (status == MagickFalse)
1695 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1696 if (p == (
const Quantum *) NULL)
1701 for (x=0; x < (ssize_t) image->columns; x++)
1710 angle=MagickPI*((u*x/(double) image->rows)+(v*y/(double) image->columns));
1711 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1712 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1714 PixelChannel channel = GetPixelChannelChannel(image,i);
1715 PixelTrait traits = GetPixelChannelTraits(image,channel);
1716 if (traits == UndefinedPixelTrait)
1718 if (channel == AlphaPixelChannel)
1720 channel_real[i]+=(QuantumScale*p[i])*cos(angle);
1721 channel_imag[i]-=(QuantumScale*p[i])*sin(angle);
1725 channel_real[i]+=(QuantumScale*Sa*p[i])*cos(angle);
1726 channel_imag[i]-=(QuantumScale*Sa*p[i])*sin(angle);
1729 p+=(ptrdiff_t) GetPixelChannels(image);
1732 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1733 phase[k]=atan2(channel_imag[k],channel_real[k]);
1734 phase[CompositePixelChannel]=atan2(channel_imag[CompositePixelChannel],
1735 channel_real[CompositePixelChannel]);
1736 image_view=DestroyCacheView(image_view);
1740static MagickBooleanType GetPHASESimilarity(
const Image *image,
1741 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1751 status = MagickTrue;
1764 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1765 image_view=AcquireVirtualCacheView(image,exception);
1766 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1767#if defined(MAGICKCORE_OPENMP_SUPPORT)
1768 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1769 magick_number_threads(image,image,rows,1)
1771 for (y=0; y < (ssize_t) rows; y++)
1779 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1784 if (status == MagickFalse)
1786 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1787 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1788 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1793 for (x=0; x < (ssize_t) columns; x++)
1796 phase[MaxPixelChannels+1] = { 0.0 },
1797 reconstruct_phase[MaxPixelChannels+1] = { 0.0 };
1802 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1803 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1805 p+=(ptrdiff_t) GetPixelChannels(image);
1806 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1809 status=DFTPhaseSpectrum(image,x,y,phase,exception);
1810 if (status == MagickFalse)
1812 status=DFTPhaseSpectrum(reconstruct_image,x,y,reconstruct_phase,
1814 if (status == MagickFalse)
1816 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1821 PixelChannel channel = GetPixelChannelChannel(image,i);
1822 PixelTrait traits = GetPixelChannelTraits(image,channel);
1823 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1825 if (((traits & UpdatePixelTrait) == 0) ||
1826 ((reconstruct_traits & UpdatePixelTrait) == 0))
1828 delta=phase[i]-reconstruct_phase[i];
1829 channel_similarity[i]+=cos(delta);
1830 channel_similarity[CompositePixelChannel]+=cos(delta);
1833 p+=(ptrdiff_t) GetPixelChannels(image);
1834 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1836#if defined(MAGICKCORE_OPENMP_SUPPORT)
1837 #pragma omp critical (MagickCore_GetPHASESimilarity)
1844 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1846 PixelChannel channel = GetPixelChannelChannel(image,j);
1847 PixelTrait traits = GetPixelChannelTraits(image,channel);
1848 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1850 if (((traits & UpdatePixelTrait) == 0) ||
1851 ((reconstruct_traits & UpdatePixelTrait) == 0))
1853 similarity[j]+=channel_similarity[j];
1855 similarity[CompositePixelChannel]+=
1856 channel_similarity[CompositePixelChannel];
1859 reconstruct_view=DestroyCacheView(reconstruct_view);
1860 image_view=DestroyCacheView(image_view);
1861 area=MagickSafeReciprocal(area);
1862 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1864 PixelChannel channel = GetPixelChannelChannel(image,k);
1865 PixelTrait traits = GetPixelChannelTraits(image,channel);
1866 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1868 if (((traits & UpdatePixelTrait) == 0) ||
1869 ((reconstruct_traits & UpdatePixelTrait) == 0))
1871 similarity[k]*=area;
1873 similarity[CompositePixelChannel]*=area;
1874 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1878static MagickBooleanType GetPSNRSimilarity(
const Image *image,
1879 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1882 status = MagickTrue;
1890 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
1891 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1893 PixelChannel channel = GetPixelChannelChannel(image,i);
1894 PixelTrait traits = GetPixelChannelTraits(image,channel);
1895 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1897 if (((traits & UpdatePixelTrait) == 0) ||
1898 ((reconstruct_traits & UpdatePixelTrait) == 0))
1900 similarity[i]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1901 similarity[i]))/MagickSafePSNRRecipicol(10.0);
1903 similarity[CompositePixelChannel]=10.0*MagickSafeLog10(
1904 MagickSafeReciprocal(similarity[CompositePixelChannel]))/
1905 MagickSafePSNRRecipicol(10.0);
1909static MagickBooleanType GetPHASHSimilarity(
const Image *image,
1910 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1912#define PHASHNormalizationFactor 389.373723242
1914 ChannelPerceptualHash
1927 channel_phash=GetImagePerceptualHash(image,exception);
1928 if (channel_phash == (ChannelPerceptualHash *) NULL)
1929 return(MagickFalse);
1930 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1931 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1933 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1935 return(MagickFalse);
1937 for (k=0; k < MaxPixelChannels; k++)
1945 PixelChannel channel = GetPixelChannelChannel(image,k);
1946 PixelTrait traits = GetPixelChannelTraits(image,channel);
1947 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1949 if (((traits & UpdatePixelTrait) == 0) ||
1950 ((reconstruct_traits & UpdatePixelTrait) == 0))
1953 for (i=0; i < MaximumNumberOfImageMoments; i++)
1962 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1967 alpha=channel_phash[k].phash[j][i];
1968 beta=reconstruct_phash[k].phash[j][i];
1970 if (IsNaN(error) != 0)
1972 difference+=error*error/PHASHNormalizationFactor;
1975 similarity[k]+=difference;
1976 similarity[CompositePixelChannel]+=difference;
1978 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1979 artifact=GetImageArtifact(image,
"phash:normalize");
1980 if (IsStringTrue(artifact) != MagickFalse)
1985 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1987 PixelChannel channel = GetPixelChannelChannel(image,j);
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[j]=sqrt(similarity[j]/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 GetRMSESimilarity(
const Image *image,
2009 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2011#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
2014 status = MagickTrue;
2022 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
2023 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2025 PixelChannel channel = GetPixelChannelChannel(image,i);
2026 PixelTrait traits = GetPixelChannelTraits(image,channel);
2027 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2029 if (((traits & UpdatePixelTrait) == 0) ||
2030 ((reconstruct_traits & UpdatePixelTrait) == 0))
2032 similarity[i]=RMSESquareRoot(similarity[i]);
2034 similarity[CompositePixelChannel]=RMSESquareRoot(
2035 similarity[CompositePixelChannel]);
2039static MagickBooleanType GetSSIMSimularity(
const Image *image,
2040 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2042#define SSIMRadius 5.0
2043#define SSIMSigma 1.5
2053 geometry[MagickPathExtent];
2069 status = MagickTrue;
2083 artifact=GetImageArtifact(image,
"compare:ssim-radius");
2084 if (artifact != (
const char *) NULL)
2085 radius=StringToDouble(artifact,(
char **) NULL);
2087 artifact=GetImageArtifact(image,
"compare:ssim-sigma");
2088 if (artifact != (
const char *) NULL)
2089 sigma=StringToDouble(artifact,(
char **) NULL);
2090 (void) FormatLocaleString(geometry,MagickPathExtent,
"gaussian:%.20gx%.20g",
2092 kernel_info=AcquireKernelInfo(geometry,exception);
2093 if (kernel_info == (KernelInfo *) NULL)
2094 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
2096 c1=pow(SSIMK1*SSIML,2.0);
2097 artifact=GetImageArtifact(image,
"compare:ssim-k1");
2098 if (artifact != (
const char *) NULL)
2099 c1=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
2100 c2=pow(SSIMK2*SSIML,2.0);
2101 artifact=GetImageArtifact(image,
"compare:ssim-k2");
2102 if (artifact != (
const char *) NULL)
2103 c2=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
2104 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2105 image_view=AcquireVirtualCacheView(image,exception);
2106 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2107#if defined(MAGICKCORE_OPENMP_SUPPORT)
2108 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
2109 magick_number_threads(image,reconstruct_image,rows,1)
2111 for (y=0; y < (ssize_t) rows; y++)
2119 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2125 if (status == MagickFalse)
2127 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
2128 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2129 kernel_info->height,exception);
2130 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
2131 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
2132 kernel_info->height,exception);
2133 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2138 for (x=0; x < (ssize_t) columns; x++)
2141 *magick_restrict reconstruct,
2142 *magick_restrict test;
2145 x_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2146 x_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 },
2147 xy_sigma[MaxPixelChannels+1] = { 0.0 },
2148 y_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2149 y_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 };
2157 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
2158 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
2160 p+=(ptrdiff_t) GetPixelChannels(image);
2161 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2164 k=kernel_info->values;
2167 for (v=0; v < (ssize_t) kernel_info->height; v++)
2172 for (u=0; u < (ssize_t) kernel_info->width; u++)
2174 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2180 PixelChannel channel = GetPixelChannelChannel(image,i);
2181 PixelTrait traits = GetPixelChannelTraits(image,channel);
2182 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2183 reconstruct_image,channel);
2184 if (((traits & UpdatePixelTrait) == 0) ||
2185 ((reconstruct_traits & UpdatePixelTrait) == 0))
2187 x_pixel=QuantumScale*(double) test[i];
2188 x_pixel_mu[i]+=(*k)*x_pixel;
2189 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
2190 y_pixel=QuantumScale*(double)
2191 GetPixelChannel(reconstruct_image,channel,reconstruct);
2192 y_pixel_mu[i]+=(*k)*y_pixel;
2193 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
2194 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
2197 test+=(ptrdiff_t) GetPixelChannels(image);
2198 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2200 test+=(ptrdiff_t) GetPixelChannels(image)*columns;
2201 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image)*columns;
2203 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2208 x_pixel_sigmas_squared,
2212 y_pixel_sigmas_squared;
2214 PixelChannel channel = GetPixelChannelChannel(image,i);
2215 PixelTrait traits = GetPixelChannelTraits(image,channel);
2216 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2217 reconstruct_image,channel);
2218 if (((traits & UpdatePixelTrait) == 0) ||
2219 ((reconstruct_traits & UpdatePixelTrait) == 0))
2221 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
2222 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
2223 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
2224 xy_sigmas=xy_sigma[i]-xy_mu;
2225 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
2226 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
2227 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))*
2228 MagickSafeReciprocal((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
2229 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
2230 channel_similarity[i]+=ssim;
2231 channel_similarity[CompositePixelChannel]+=ssim;
2233 p+=(ptrdiff_t) GetPixelChannels(image);
2234 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2237#if defined(MAGICKCORE_OPENMP_SUPPORT)
2238 #pragma omp critical (MagickCore_GetSSIMSimularity)
2245 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2247 PixelChannel channel = GetPixelChannelChannel(image,j);
2248 PixelTrait traits = GetPixelChannelTraits(image,channel);
2249 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2251 if (((traits & UpdatePixelTrait) == 0) ||
2252 ((reconstruct_traits & UpdatePixelTrait) == 0))
2254 similarity[j]+=channel_similarity[j];
2256 similarity[CompositePixelChannel]+=
2257 channel_similarity[CompositePixelChannel];
2260 image_view=DestroyCacheView(image_view);
2261 reconstruct_view=DestroyCacheView(reconstruct_view);
2262 area=MagickSafeReciprocal(area);
2263 for (l=0; l < (ssize_t) GetPixelChannels(image); l++)
2265 PixelChannel channel = GetPixelChannelChannel(image,l);
2266 PixelTrait traits = GetPixelChannelTraits(image,channel);
2267 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2269 if (((traits & UpdatePixelTrait) == 0) ||
2270 ((reconstruct_traits & UpdatePixelTrait) == 0))
2272 similarity[l]*=area;
2274 similarity[CompositePixelChannel]*=area;
2275 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
2276 kernel_info=DestroyKernelInfo(kernel_info);
2280static MagickBooleanType GetDSSIMSimilarity(
const Image *image,
2281 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2284 status = MagickTrue;
2292 status=GetSSIMSimularity(image,reconstruct_image,similarity,exception);
2293 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2295 PixelChannel channel = GetPixelChannelChannel(image,i);
2296 PixelTrait traits = GetPixelChannelTraits(image,channel);
2297 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2299 if (((traits & UpdatePixelTrait) == 0) ||
2300 ((reconstruct_traits & UpdatePixelTrait) == 0))
2302 similarity[i]=(1.0-similarity[i])/2.0;
2304 similarity[CompositePixelChannel]=(1.0-similarity[CompositePixelChannel])/2.0;
2308MagickExport MagickBooleanType GetImageDistortion(Image *image,
2309 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
2310 ExceptionInfo *exception)
2312#define CompareMetricNotSupportedException "metric not supported"
2315 *channel_similarity;
2318 status = MagickTrue;
2323 assert(image != (Image *) NULL);
2324 assert(image->signature == MagickCoreSignature);
2325 assert(reconstruct_image != (
const Image *) NULL);
2326 assert(reconstruct_image->signature == MagickCoreSignature);
2327 assert(distortion != (
double *) NULL);
2328 if (IsEventLogging() != MagickFalse)
2329 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2334 length=MaxPixelChannels+1UL;
2335 channel_similarity=(
double *) AcquireQuantumMemory(length,
2336 sizeof(*channel_similarity));
2337 if (channel_similarity == (
double *) NULL)
2338 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2339 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2342 case AbsoluteErrorMetric:
2344 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2348 case DotProductCorrelationErrorMetric:
2350 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2354 case FuzzErrorMetric:
2356 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2360 case MeanAbsoluteErrorMetric:
2362 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2366 case MeanErrorPerPixelErrorMetric:
2368 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2372 case MeanSquaredErrorMetric:
2374 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2378 case NormalizedCrossCorrelationErrorMetric:
2380 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2384 case PeakAbsoluteErrorMetric:
2386 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2390 case PeakSignalToNoiseRatioErrorMetric:
2392 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2396 case PerceptualHashErrorMetric:
2398 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2402 case PhaseCorrelationErrorMetric:
2404 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2408 case PixelDifferenceCountErrorMetric:
2410 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2414 case RootMeanSquaredErrorMetric:
2415 case UndefinedErrorMetric:
2418 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2422 case StructuralDissimilarityErrorMetric:
2424 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2428 case StructuralSimilarityErrorMetric:
2430 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2435 *distortion=channel_similarity[CompositePixelChannel];
2438 case DotProductCorrelationErrorMetric:
2439 case NormalizedCrossCorrelationErrorMetric:
2440 case PhaseCorrelationErrorMetric:
2441 case StructuralSimilarityErrorMetric:
2443 *distortion=(1.0-(*distortion))/2.0;
2448 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2449 if (fabs(*distortion) < MagickEpsilon)
2451 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2487MagickExport
double *GetImageDistortions(Image *image,
2488 const Image *reconstruct_image,
const MetricType metric,
2489 ExceptionInfo *exception)
2493 *channel_similarity;
2496 status = MagickTrue;
2504 assert(image != (Image *) NULL);
2505 assert(image->signature == MagickCoreSignature);
2506 assert(reconstruct_image != (
const Image *) NULL);
2507 assert(reconstruct_image->signature == MagickCoreSignature);
2508 if (IsEventLogging() != MagickFalse)
2509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2513 length=MaxPixelChannels+1UL;
2514 channel_similarity=(
double *) AcquireQuantumMemory(length,
2515 sizeof(*channel_similarity));
2516 if (channel_similarity == (
double *) NULL)
2517 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2518 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2521 case AbsoluteErrorMetric:
2523 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2527 case DotProductCorrelationErrorMetric:
2529 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2533 case FuzzErrorMetric:
2535 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2539 case MeanAbsoluteErrorMetric:
2541 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2545 case MeanErrorPerPixelErrorMetric:
2547 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2551 case MeanSquaredErrorMetric:
2553 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2557 case NormalizedCrossCorrelationErrorMetric:
2559 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2563 case PeakAbsoluteErrorMetric:
2565 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2569 case PeakSignalToNoiseRatioErrorMetric:
2571 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2575 case PerceptualHashErrorMetric:
2577 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2581 case PhaseCorrelationErrorMetric:
2583 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2587 case PixelDifferenceCountErrorMetric:
2589 status=GetPDCSimilarity(image,reconstruct_image,channel_similarity,
2593 case RootMeanSquaredErrorMetric:
2594 case UndefinedErrorMetric:
2597 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2601 case StructuralDissimilarityErrorMetric:
2603 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2607 case StructuralSimilarityErrorMetric:
2609 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2614 if (status == MagickFalse)
2616 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2617 return((
double *) NULL);
2619 distortion=channel_similarity;
2622 case DotProductCorrelationErrorMetric:
2623 case NormalizedCrossCorrelationErrorMetric:
2624 case PhaseCorrelationErrorMetric:
2625 case StructuralSimilarityErrorMetric:
2627 for (i=0; i <= MaxPixelChannels; i++)
2628 distortion[i]=(1.0-distortion[i])/2.0;
2633 for (i=0; i <= MaxPixelChannels; i++)
2634 if (fabs(distortion[i]) < MagickEpsilon)
2636 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2637 distortion[CompositePixelChannel]);
2669MagickExport MagickBooleanType IsImagesEqual(
const Image *image,
2670 const Image *reconstruct_image,ExceptionInfo *exception)
2683 assert(image != (Image *) NULL);
2684 assert(image->signature == MagickCoreSignature);
2685 assert(reconstruct_image != (
const Image *) NULL);
2686 assert(reconstruct_image->signature == MagickCoreSignature);
2687 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2688 image_view=AcquireVirtualCacheView(image,exception);
2689 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2690 for (y=0; y < (ssize_t) rows; y++)
2699 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2700 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2701 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2703 for (x=0; x < (ssize_t) columns; x++)
2708 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2713 PixelChannel channel = GetPixelChannelChannel(image,i);
2714 PixelTrait traits = GetPixelChannelTraits(image,channel);
2715 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2717 if (((traits & UpdatePixelTrait) == 0) ||
2718 ((reconstruct_traits & UpdatePixelTrait) == 0))
2720 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
2722 if (distance >= MagickEpsilon)
2725 if (i < (ssize_t) GetPixelChannels(image))
2727 p+=(ptrdiff_t) GetPixelChannels(image);
2728 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2730 if (x < (ssize_t) columns)
2733 reconstruct_view=DestroyCacheView(reconstruct_view);
2734 image_view=DestroyCacheView(image_view);
2735 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
2787MagickExport MagickBooleanType SetImageColorMetric(Image *image,
2788 const Image *reconstruct_image,ExceptionInfo *exception)
2791 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2796 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2798 if (status == MagickFalse)
2799 return(MagickFalse);
2800 status=fabs(image->error.mean_error_per_pixel) < MagickEpsilon ?
2801 MagickTrue : MagickFalse;
2848#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2849static Image *SIMCrossCorrelationImage(
const Image *alpha_image,
2850 const Image *beta_image,ExceptionInfo *exception)
2853 *alpha_fft = (Image *) NULL,
2854 *beta_fft = (Image *) NULL,
2855 *complex_conjugate = (Image *) NULL,
2856 *complex_multiplication = (Image *) NULL,
2857 *cross_correlation = (Image *) NULL,
2858 *temp_image = (Image *) NULL;
2863 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2864 if (temp_image == (Image *) NULL)
2865 return((Image *) NULL);
2866 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2867 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2868 temp_image=DestroyImageList(temp_image);
2869 if (beta_fft == (Image *) NULL)
2870 return((Image *) NULL);
2874 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2875 beta_fft=DestroyImageList(beta_fft);
2876 if (complex_conjugate == (Image *) NULL)
2877 return((Image *) NULL);
2881 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2882 if (temp_image == (Image *) NULL)
2884 complex_conjugate=DestroyImageList(complex_conjugate);
2885 return((Image *) NULL);
2887 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2888 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2889 temp_image=DestroyImageList(temp_image);
2890 if (alpha_fft == (Image *) NULL)
2892 complex_conjugate=DestroyImageList(complex_conjugate);
2893 return((Image *) NULL);
2898 DisableCompositeClampUnlessSpecified(complex_conjugate);
2899 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2900 AppendImageToList(&complex_conjugate,alpha_fft);
2901 complex_multiplication=ComplexImages(complex_conjugate,
2902 MultiplyComplexOperator,exception);
2903 complex_conjugate=DestroyImageList(complex_conjugate);
2904 if (complex_multiplication == (Image *) NULL)
2905 return((Image *) NULL);
2909 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2910 complex_multiplication->next,MagickFalse,exception);
2911 complex_multiplication=DestroyImageList(complex_multiplication);
2912 return(cross_correlation);
2915static Image *SIMDerivativeImage(
const Image *image,
const char *kernel,
2916 ExceptionInfo *exception)
2924 kernel_info=AcquireKernelInfo(kernel,exception);
2925 if (kernel_info == (KernelInfo *) NULL)
2926 return((Image *) NULL);
2927 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2929 kernel_info=DestroyKernelInfo(kernel_info);
2930 return(derivative_image);
2933static Image *SIMDivideImage(
const Image *numerator_image,
2934 const Image *denominator_image,ExceptionInfo *exception)
2944 status = MagickTrue;
2952 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2953 if (divide_image == (Image *) NULL)
2954 return(divide_image);
2955 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2956 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2957#if defined(MAGICKCORE_OPENMP_SUPPORT)
2958 #pragma omp parallel for schedule(static) shared(status) \
2959 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
2961 for (y=0; y < (ssize_t) divide_image->rows; y++)
2972 if (status == MagickFalse)
2974 p=GetCacheViewVirtualPixels(denominator_view,0,y,
2975 denominator_image->columns,1,exception);
2976 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
2978 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2983 for (x=0; x < (ssize_t) divide_image->columns; x++)
2988 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
2990 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
2991 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
2992 PixelTrait denominator_traits = GetPixelChannelTraits(denominator_image,
2994 if (((traits & UpdatePixelTrait) == 0) ||
2995 ((denominator_traits & UpdatePixelTrait) == 0))
2997 q[i]=(Quantum) ((
double) q[i]*MagickSafeReciprocal(QuantumScale*
2998 (
double) GetPixelChannel(denominator_image,channel,p)));
3000 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
3001 q+=(ptrdiff_t) GetPixelChannels(divide_image);
3003 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
3006 denominator_view=DestroyCacheView(denominator_view);
3007 numerator_view=DestroyCacheView(numerator_view);
3008 if (status == MagickFalse)
3009 divide_image=DestroyImage(divide_image);
3010 return(divide_image);
3013static Image *SIMDivideByMagnitude(Image *image,Image *magnitude_image,
3014 const Image *source_image,ExceptionInfo *exception)
3023 divide_image=SIMDivideImage(image,magnitude_image,exception);
3024 if (divide_image == (Image *) NULL)
3025 return((Image *) NULL);
3026 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
3027 ÷_image->background_color);
3028 SetGeometry(source_image,&geometry);
3029 geometry.width=MagickMax(source_image->columns,divide_image->columns);
3030 geometry.height=MagickMax(source_image->rows,divide_image->rows);
3031 result_image=ExtentImage(divide_image,&geometry,exception);
3032 divide_image=DestroyImage(divide_image);
3033 return(result_image);
3036static MagickBooleanType SIMFilterImageNaNs(Image *image,
3037 ExceptionInfo *exception)
3043 status = MagickTrue;
3051 image_view=AcquireAuthenticCacheView(image,exception);
3052#if defined(MAGICKCORE_OPENMP_SUPPORT)
3053 #pragma omp parallel for schedule(static) shared(status) \
3054 magick_number_threads(image,image,image->rows,1)
3056 for (y=0; y < (ssize_t) image->rows; y++)
3064 if (status == MagickFalse)
3066 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3067 if (q == (Quantum *) NULL)
3072 for (x=0; x < (ssize_t) image->columns; x++)
3077 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3079 PixelChannel channel = GetPixelChannelChannel(image,i);
3080 PixelTrait traits = GetPixelChannelTraits(image,channel);
3081 if ((traits & UpdatePixelTrait) == 0)
3083 if (IsNaN((
double) q[i]) != 0)
3086 q+=(ptrdiff_t) GetPixelChannels(image);
3088 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3091 image_view=DestroyCacheView(image_view);
3095static Image *SIMSquareImage(
const Image *image,ExceptionInfo *exception)
3104 status = MagickTrue;
3112 square_image=CloneImage(image,0,0,MagickTrue,exception);
3113 if (square_image == (Image *) NULL)
3114 return(square_image);
3115 image_view=AcquireAuthenticCacheView(square_image,exception);
3116#if defined(MAGICKCORE_OPENMP_SUPPORT)
3117 #pragma omp parallel for schedule(static) shared(status) \
3118 magick_number_threads(square_image,square_image,square_image->rows,1)
3120 for (y=0; y < (ssize_t) square_image->rows; y++)
3128 if (status == MagickFalse)
3130 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
3132 if (q == (Quantum *) NULL)
3137 for (x=0; x < (ssize_t) square_image->columns; x++)
3142 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
3144 PixelChannel channel = GetPixelChannelChannel(square_image,i);
3145 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
3146 if ((traits & UpdatePixelTrait) == 0)
3148 q[i]=(Quantum) (QuantumScale*q[i]*q[i]);
3150 q+=(ptrdiff_t) GetPixelChannels(square_image);
3152 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3155 image_view=DestroyCacheView(image_view);
3156 if (status == MagickFalse)
3157 square_image=DestroyImage(square_image);
3158 return(square_image);
3161static Image *SIMMagnitudeImage(Image *alpha_image,Image *beta_image,
3162 ExceptionInfo *exception)
3170 status = MagickTrue;
3172 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
3173 xsq_image=SIMSquareImage(alpha_image,exception);
3174 if (xsq_image == (Image *) NULL)
3175 return((Image *) NULL);
3176 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
3177 ysq_image=SIMSquareImage(beta_image,exception);
3178 if (ysq_image == (Image *) NULL)
3180 xsq_image=DestroyImage(xsq_image);
3181 return((Image *) NULL);
3183 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
3185 magnitude_image=xsq_image;
3186 ysq_image=DestroyImage(ysq_image);
3187 if (status == MagickFalse)
3189 magnitude_image=DestroyImage(magnitude_image);
3190 return((Image *) NULL);
3192 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
3193 if (status == MagickFalse)
3195 magnitude_image=DestroyImage(magnitude_image);
3196 return (Image *) NULL;
3198 return(magnitude_image);
3201static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
3202 RectangleInfo *offset,ExceptionInfo *exception)
3221 status = MagickTrue;
3224 maxima_info = { -MagickMaximumValue, 0, 0 };
3232 image_view=AcquireVirtualCacheView(image,exception);
3233 q=GetCacheViewVirtualPixels(image_view,maxima_info.x,maxima_info.y,1,1,
3235 if (q != (
const Quantum *) NULL)
3236 maxima_info.maxima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3237#if defined(MAGICKCORE_OPENMP_SUPPORT)
3238 #pragma omp parallel for schedule(static) shared(maxima_info,status) \
3239 magick_number_threads(image,image,image->rows,1)
3241 for (y=0; y < (ssize_t) image->rows; y++)
3247 channel_maxima = { -MagickMaximumValue, 0, 0 };
3252 if (status == MagickFalse)
3254 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3255 if (p == (
const Quantum *) NULL)
3260 channel_maxima=maxima_info;
3261 for (x=0; x < (ssize_t) image->columns; x++)
3266 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3271 PixelChannel channel = GetPixelChannelChannel(image,i);
3272 PixelTrait traits = GetPixelChannelTraits(image,channel);
3273 if ((traits & UpdatePixelTrait) == 0)
3275 pixel=(double) p[i];
3276 if (IsNaN(pixel) != 0)
3278 if (pixel > channel_maxima.maxima)
3280 channel_maxima.maxima=(double) p[i];
3285 p+=(ptrdiff_t) GetPixelChannels(image);
3287#if defined(MAGICKCORE_OPENMP_SUPPORT)
3288 #pragma omp critical (MagickCore_SIMMaximaImage)
3290 if (channel_maxima.maxima > maxima_info.maxima)
3291 maxima_info=channel_maxima;
3293 image_view=DestroyCacheView(image_view);
3294 *maxima=maxima_info.maxima;
3295 offset->x=maxima_info.x;
3296 offset->y=maxima_info.y;
3300static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
3301 RectangleInfo *offset,ExceptionInfo *exception)
3320 status = MagickTrue;
3323 minima_info = { MagickMaximumValue, 0, 0 };
3331 image_view=AcquireVirtualCacheView(image,exception);
3332 q=GetCacheViewVirtualPixels(image_view,minima_info.x,minima_info.y,1,1,
3334 if (q != (
const Quantum *) NULL)
3335 minima_info.minima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3336#if defined(MAGICKCORE_OPENMP_SUPPORT)
3337 #pragma omp parallel for schedule(static) shared(minima_info,status) \
3338 magick_number_threads(image,image,image->rows,1)
3340 for (y=0; y < (ssize_t) image->rows; y++)
3346 channel_minima = { MagickMaximumValue, 0, 0 };
3351 if (status == MagickFalse)
3353 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3354 if (p == (
const Quantum *) NULL)
3359 channel_minima=minima_info;
3360 for (x=0; x < (ssize_t) image->columns; x++)
3365 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3370 PixelChannel channel = GetPixelChannelChannel(image,i);
3371 PixelTrait traits = GetPixelChannelTraits(image,channel);
3372 if ((traits & UpdatePixelTrait) == 0)
3374 pixel=(double) p[i];
3375 if (IsNaN(pixel) != 0)
3377 if (pixel < channel_minima.minima)
3379 channel_minima.minima=pixel;
3384 p+=(ptrdiff_t) GetPixelChannels(image);
3386#if defined(MAGICKCORE_OPENMP_SUPPORT)
3387 #pragma omp critical (MagickCore_SIMMinimaImage)
3389 if (channel_minima.minima < minima_info.minima)
3390 minima_info=channel_minima;
3392 image_view=DestroyCacheView(image_view);
3393 *minima=minima_info.minima;
3394 offset->x=minima_info.x;
3395 offset->y=minima_info.y;
3399static MagickBooleanType SIMMultiplyImage(Image *image,
const double factor,
3400 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3406 status = MagickTrue;
3414 image_view=AcquireAuthenticCacheView(image,exception);
3415#if defined(MAGICKCORE_OPENMP_SUPPORT)
3416 #pragma omp parallel for schedule(static) shared(status) \
3417 magick_number_threads(image,image,image->rows,1)
3419 for (y=0; y < (ssize_t) image->rows; y++)
3427 if (status == MagickFalse)
3429 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3430 if (q == (Quantum *) NULL)
3435 for (x=0; x < (ssize_t) image->columns; x++)
3440 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3442 PixelChannel channel = GetPixelChannelChannel(image,i);
3443 PixelTrait traits = GetPixelChannelTraits(image,channel);
3444 if ((traits & UpdatePixelTrait) == 0)
3446 if (channel_statistics != (
const ChannelStatistics *) NULL)
3447 q[i]=(Quantum) (factor*q[i]*QuantumScale*
3448 channel_statistics[channel].standard_deviation);
3450 q[i]=(Quantum) (factor*q[i]);
3452 q+=(ptrdiff_t) GetPixelChannels(image);
3454 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3457 image_view=DestroyCacheView(image_view);
3461static Image *SIMPhaseCorrelationImage(
const Image *alpha_image,
3462 const Image *beta_image,
const Image *magnitude_image,ExceptionInfo *exception)
3465 *alpha_fft = (Image *) NULL,
3466 *beta_fft = (Image *) NULL,
3467 *complex_multiplication = (Image *) NULL,
3468 *cross_correlation = (Image *) NULL;
3473 beta_fft=CloneImage(beta_image,0,0,MagickTrue,exception);
3474 if (beta_fft == NULL)
3475 return((Image *) NULL);
3476 (void) SetImageArtifact(beta_fft,
"fourier:normalize",
"inverse");
3477 beta_fft=ForwardFourierTransformImage(beta_fft,MagickFalse,exception);
3478 if (beta_fft == NULL)
3479 return((Image *) NULL);
3483 alpha_fft=CloneImage(alpha_image,0,0,MagickTrue,exception);
3484 if (alpha_fft == (Image *) NULL)
3486 beta_fft=DestroyImageList(beta_fft);
3487 return((Image *) NULL);
3489 (void) SetImageArtifact(alpha_fft,
"fourier:normalize",
"inverse");
3490 alpha_fft=ForwardFourierTransformImage(alpha_fft,MagickFalse,exception);
3491 if (alpha_fft == (Image *) NULL)
3493 beta_fft=DestroyImageList(beta_fft);
3494 return((Image *) NULL);
3499 beta_fft=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
3500 if (beta_fft == (Image *) NULL)
3502 alpha_fft=DestroyImageList(alpha_fft);
3503 return((Image *) NULL);
3508 AppendImageToList(&beta_fft,alpha_fft);
3509 DisableCompositeClampUnlessSpecified(beta_fft);
3510 DisableCompositeClampUnlessSpecified(beta_fft->next);
3511 complex_multiplication=ComplexImages(beta_fft,MultiplyComplexOperator,
3513 beta_fft=DestroyImageList(beta_fft);
3514 if (complex_multiplication == (Image *) NULL)
3515 return((Image *) NULL);
3519 CompositeLayers(complex_multiplication,DivideSrcCompositeOp,(Image *)
3520 magnitude_image,0,0,exception);
3524 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
3525 cross_correlation=InverseFourierTransformImage(complex_multiplication,
3526 complex_multiplication->next,MagickFalse,exception);
3527 complex_multiplication=DestroyImageList(complex_multiplication);
3528 return(cross_correlation);
3531static MagickBooleanType SIMSetImageMean(Image *image,
3532 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3538 status = MagickTrue;
3546 image_view=AcquireAuthenticCacheView(image,exception);
3547#if defined(MAGICKCORE_OPENMP_SUPPORT)
3548 #pragma omp parallel for schedule(static) shared(status) \
3549 magick_number_threads(image,image,image->rows,1)
3551 for (y=0; y < (ssize_t) image->rows; y++)
3559 if (status == MagickFalse)
3561 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3562 if (q == (Quantum *) NULL)
3567 for (x=0; x < (ssize_t) image->columns; x++)
3572 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3574 PixelChannel channel = GetPixelChannelChannel(image,i);
3575 PixelTrait traits = GetPixelChannelTraits(image,channel);
3576 if ((traits & UpdatePixelTrait) == 0)
3578 q[i]=(Quantum) channel_statistics[channel].mean;
3580 q+=(ptrdiff_t) GetPixelChannels(image);
3582 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3585 image_view=DestroyCacheView(image_view);
3589static Image *SIMSubtractImageMean(
const Image *alpha_image,
3590 const Image *beta_image,
const ChannelStatistics *channel_statistics,
3591 ExceptionInfo *exception)
3601 status = MagickTrue;
3609 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
3610 MagickTrue,exception);
3611 if (subtract_image == (Image *) NULL)
3612 return(subtract_image);
3613 image_view=AcquireAuthenticCacheView(subtract_image,exception);
3614 beta_view=AcquireVirtualCacheView(beta_image,exception);
3615#if defined(MAGICKCORE_OPENMP_SUPPORT)
3616 #pragma omp parallel for schedule(static) shared(status) \
3617 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
3619 for (y=0; y < (ssize_t) subtract_image->rows; y++)
3630 if (status == MagickFalse)
3632 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
3633 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
3635 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3640 for (x=0; x < (ssize_t) subtract_image->columns; x++)
3645 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
3647 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
3648 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
3649 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3650 if (((traits & UpdatePixelTrait) == 0) ||
3651 ((beta_traits & UpdatePixelTrait) == 0))
3653 if ((x >= (ssize_t) beta_image->columns) ||
3654 (y >= (ssize_t) beta_image->rows))
3657 q[i]=(Quantum) ((
double) GetPixelChannel(beta_image,channel,p)-
3658 channel_statistics[channel].mean);
3660 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3661 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
3663 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3666 beta_view=DestroyCacheView(beta_view);
3667 image_view=DestroyCacheView(image_view);
3668 if (status == MagickFalse)
3669 subtract_image=DestroyImage(subtract_image);
3670 return(subtract_image);
3673static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
3674 ExceptionInfo *exception)
3683 status = MagickTrue;
3691 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
3692 MagickTrue,exception);
3693 if (unity_image == (Image *) NULL)
3694 return(unity_image);
3695 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
3696 return(DestroyImage(unity_image));
3697 image_view=AcquireAuthenticCacheView(unity_image,exception);
3698#if defined(MAGICKCORE_OPENMP_SUPPORT)
3699 #pragma omp parallel for schedule(static) shared(status) \
3700 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
3702 for (y=0; y < (ssize_t) unity_image->rows; y++)
3710 if (status == MagickFalse)
3712 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
3714 if (q == (Quantum *) NULL)
3719 for (x=0; x < (ssize_t) unity_image->columns; x++)
3724 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
3726 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
3727 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
3728 if ((traits & UpdatePixelTrait) == 0)
3730 if ((x >= (ssize_t) beta_image->columns) ||
3731 (y >= (ssize_t) beta_image->rows))
3736 q+=(ptrdiff_t) GetPixelChannels(unity_image);
3738 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3741 image_view=DestroyCacheView(image_view);
3742 if (status == MagickFalse)
3743 unity_image=DestroyImage(unity_image);
3744 return(unity_image);
3747static Image *SIMVarianceImage(Image *alpha_image,
const Image *beta_image,
3748 ExceptionInfo *exception)
3758 status = MagickTrue;
3766 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
3767 if (variance_image == (Image *) NULL)
3768 return(variance_image);
3769 image_view=AcquireAuthenticCacheView(variance_image,exception);
3770 beta_view=AcquireVirtualCacheView(beta_image,exception);
3771#if defined(MAGICKCORE_OPENMP_SUPPORT)
3772 #pragma omp parallel for schedule(static) shared(status) \
3773 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3775 for (y=0; y < (ssize_t) variance_image->rows; y++)
3786 if (status == MagickFalse)
3788 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3790 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3792 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3797 for (x=0; x < (ssize_t) variance_image->columns; x++)
3802 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3807 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3808 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3809 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3810 if (((traits & UpdatePixelTrait) == 0) ||
3811 ((beta_traits & UpdatePixelTrait) == 0))
3813 error=(double) q[i]-(
double) GetPixelChannel(beta_image,channel,p);
3814 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3815 (sqrt(fabs(QuantumScale*error))/sqrt((
double) QuantumRange))));
3817 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3818 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3820 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3823 beta_view=DestroyCacheView(beta_view);
3824 image_view=DestroyCacheView(image_view);
3825 if (status == MagickFalse)
3826 variance_image=DestroyImage(variance_image);
3827 return(variance_image);
3830static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3831 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
3833#define ThrowDPCSimilarityException() \
3835 if (dot_product_image != (Image *) NULL) \
3836 dot_product_image=DestroyImage(dot_product_image); \
3837 if (magnitude_image != (Image *) NULL) \
3838 magnitude_image=DestroyImage(magnitude_image); \
3839 if (reconstruct_image != (Image *) NULL) \
3840 reconstruct_image=DestroyImage(reconstruct_image); \
3841 if (rx_image != (Image *) NULL) \
3842 rx_image=DestroyImage(rx_image); \
3843 if (ry_image != (Image *) NULL) \
3844 ry_image=DestroyImage(ry_image); \
3845 if (target_image != (Image *) NULL) \
3846 target_image=DestroyImage(target_image); \
3847 if (threshold_image != (Image *) NULL) \
3848 threshold_image=DestroyImage(threshold_image); \
3849 if (trx_image != (Image *) NULL) \
3850 trx_image=DestroyImage(trx_image); \
3851 if (try_image != (Image *) NULL) \
3852 try_image=DestroyImage(try_image); \
3853 if (tx_image != (Image *) NULL) \
3854 tx_image=DestroyImage(tx_image); \
3855 if (ty_image != (Image *) NULL) \
3856 ty_image=DestroyImage(ty_image); \
3857 return((Image *) NULL); \
3864 standard_deviation = 0.0;
3867 *dot_product_image = (Image *) NULL,
3868 *magnitude_image = (Image *) NULL,
3869 *reconstruct_image = (Image *) NULL,
3870 *rx_image = (Image *) NULL,
3871 *ry_image = (Image *) NULL,
3872 *trx_image = (Image *) NULL,
3873 *target_image = (Image *) NULL,
3874 *threshold_image = (Image *) NULL,
3875 *try_image = (Image *) NULL,
3876 *tx_image = (Image *) NULL,
3877 *ty_image = (Image *) NULL;
3880 status = MagickTrue;
3888 target_image=CloneImage(image,0,0,MagickTrue,exception);
3889 if (target_image == (Image *) NULL)
3890 return((Image *) NULL);
3894 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3895 if (reconstruct_image == (Image *) NULL)
3896 ThrowDPCSimilarityException();
3900 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3902 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3903 if (rx_image == (Image *) NULL)
3904 ThrowDPCSimilarityException();
3905 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3906 reconstruct_image=DestroyImage(reconstruct_image);
3907 if (ry_image == (Image *) NULL)
3908 ThrowDPCSimilarityException();
3912 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3913 if (magnitude_image == (Image *) NULL)
3914 ThrowDPCSimilarityException();
3918 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3919 if (threshold_image == (Image *) NULL)
3920 ThrowDPCSimilarityException();
3921 status=BilevelImage(threshold_image,0.0,exception);
3922 if (status == MagickFalse)
3923 ThrowDPCSimilarityException();
3924 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3925 threshold_image=DestroyImage(threshold_image);
3926 if (status == MagickFalse)
3927 ThrowDPCSimilarityException();
3928 edge_factor=MagickSafeReciprocal(QuantumScale*mean*reconstruct->columns*
3929 reconstruct->rows)+QuantumScale;
3933 trx_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3934 rx_image=DestroyImage(rx_image);
3935 if (trx_image == (Image *) NULL)
3936 ThrowDPCSimilarityException();
3938 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3939 magnitude_image=DestroyImage(magnitude_image);
3940 ry_image=DestroyImage(ry_image);
3941 if (try_image == (Image *) NULL)
3942 ThrowDPCSimilarityException();
3947 (void) SetImageVirtualPixelMethod(target_image,EdgeVirtualPixelMethod,
3949 tx_image=SIMDerivativeImage(target_image,
"Sobel",exception);
3950 if (tx_image == (Image *) NULL)
3951 ThrowDPCSimilarityException();
3952 ty_image=SIMDerivativeImage(target_image,
"Sobel:90",exception);
3953 target_image=DestroyImage(target_image);
3954 if (ty_image == (Image *) NULL)
3955 ThrowDPCSimilarityException();
3959 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
3960 if (magnitude_image == (Image *) NULL)
3961 ThrowDPCSimilarityException();
3965 trx_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
3966 tx_image=DestroyImage(tx_image);
3967 if (trx_image == (Image *) NULL)
3968 ThrowDPCSimilarityException();
3970 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
3971 ty_image=DestroyImage(ty_image);
3972 magnitude_image=DestroyImage(magnitude_image);
3973 if (try_image == (Image *) NULL)
3974 ThrowDPCSimilarityException();
3979 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
3980 rx_image=DestroyImage(rx_image);
3981 tx_image=DestroyImage(tx_image);
3982 if (trx_image == (Image *) NULL)
3983 ThrowDPCSimilarityException();
3984 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
3985 ry_image=DestroyImage(ry_image);
3986 ty_image=DestroyImage(ty_image);
3987 if (try_image == (Image *) NULL)
3988 ThrowDPCSimilarityException();
3992 (void) SetImageArtifact(try_image,
"compose:clamp",
"false");
3993 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
3995 try_image=DestroyImage(try_image);
3996 if (status == MagickFalse)
3997 ThrowDPCSimilarityException();
3998 status=SIMMultiplyImage(trx_image,edge_factor,
3999 (
const ChannelStatistics *) NULL,exception);
4000 if (status == MagickFalse)
4001 ThrowDPCSimilarityException();
4005 SetGeometry(image,&geometry);
4006 geometry.width=image->columns;
4007 geometry.height=image->rows;
4008 (void) ResetImagePage(trx_image,
"0x0+0+0");
4009 dot_product_image=CropImage(trx_image,&geometry,exception);
4010 trx_image=DestroyImage(trx_image);
4011 if (dot_product_image == (Image *) NULL)
4012 ThrowDPCSimilarityException();
4013 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
4017 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
4019 if (status == MagickFalse)
4020 ThrowDPCSimilarityException();
4021 dot_product_image->depth=32;
4022 dot_product_image->colorspace=GRAYColorspace;
4023 dot_product_image->alpha_trait=UndefinedPixelTrait;
4024 status=SIMFilterImageNaNs(dot_product_image,exception);
4025 if (status == MagickFalse)
4026 ThrowDPCSimilarityException();
4027 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
4028 if (status == MagickFalse)
4029 ThrowDPCSimilarityException();
4030 if ((QuantumScale*maxima) > 1.0)
4032 status=SIMMultiplyImage(dot_product_image,1.0/(QuantumScale*maxima),
4033 (
const ChannelStatistics *) NULL,exception);
4034 maxima=(double) QuantumRange;
4036 *similarity_metric=QuantumScale*maxima;
4037 return(dot_product_image);
4040static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
4041 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4043#define ThrowMSESimilarityException() \
4045 if (alpha_image != (Image *) NULL) \
4046 alpha_image=DestroyImage(alpha_image); \
4047 if (beta_image != (Image *) NULL) \
4048 beta_image=DestroyImage(beta_image); \
4049 if (channel_statistics != (ChannelStatistics *) NULL) \
4050 channel_statistics=(ChannelStatistics *) \
4051 RelinquishMagickMemory(channel_statistics); \
4052 if (mean_image != (Image *) NULL) \
4053 mean_image=DestroyImage(mean_image); \
4054 if (mse_image != (Image *) NULL) \
4055 mse_image=DestroyImage(mse_image); \
4056 if (reconstruct_image != (Image *) NULL) \
4057 reconstruct_image=DestroyImage(reconstruct_image); \
4058 if (sum_image != (Image *) NULL) \
4059 sum_image=DestroyImage(sum_image); \
4060 if (alpha_image != (Image *) NULL) \
4061 alpha_image=DestroyImage(alpha_image); \
4062 return((Image *) NULL); \
4066 *channel_statistics = (ChannelStatistics *) NULL;
4072 *alpha_image = (Image *) NULL,
4073 *beta_image = (Image *) NULL,
4074 *mean_image = (Image *) NULL,
4075 *mse_image = (Image *) NULL,
4076 *reconstruct_image = (Image *) NULL,
4077 *sum_image = (Image *) NULL,
4078 *target_image = (Image *) NULL;
4081 status = MagickTrue;
4089 target_image=SIMSquareImage(image,exception);
4090 if (target_image == (Image *) NULL)
4091 ThrowMSESimilarityException();
4092 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4093 if (reconstruct_image == (Image *) NULL)
4094 ThrowMSESimilarityException();
4098 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4100 target_image=DestroyImage(target_image);
4101 if (alpha_image == (Image *) NULL)
4102 ThrowMSESimilarityException();
4103 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
4104 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
4105 if (status == MagickFalse)
4106 ThrowMSESimilarityException();
4110 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
4111 MagickTrue,0,0,exception);
4112 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4113 reconstruct_image=DestroyImage(reconstruct_image);
4114 if (beta_image == (Image *) NULL)
4115 ThrowMSESimilarityException();
4116 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
4117 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
4118 if (status == MagickFalse)
4119 ThrowMSESimilarityException();
4123 sum_image=SIMSquareImage(reconstruct,exception);
4124 if (sum_image == (Image *) NULL)
4125 ThrowMSESimilarityException();
4126 channel_statistics=GetImageStatistics(sum_image,exception);
4127 if (channel_statistics == (ChannelStatistics *) NULL)
4128 ThrowMSESimilarityException();
4129 status=SetImageStorageClass(sum_image,DirectClass,exception);
4130 if (status == MagickFalse)
4131 ThrowMSESimilarityException();
4132 status=SIMSetImageMean(sum_image,channel_statistics,exception);
4133 channel_statistics=(ChannelStatistics *)
4134 RelinquishMagickMemory(channel_statistics);
4135 if (status == MagickFalse)
4136 ThrowMSESimilarityException();
4140 AppendImageToList(&sum_image,alpha_image);
4141 AppendImageToList(&sum_image,beta_image);
4142 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
4143 if (mean_image == (Image *) NULL)
4144 ThrowMSESimilarityException();
4145 sum_image=DestroyImage(sum_image);
4146 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
4147 if (status == MagickFalse)
4148 ThrowMSESimilarityException();
4152 SetGeometry(image,&geometry);
4153 geometry.width=image->columns;
4154 geometry.height=image->rows;
4155 (void) ResetImagePage(mean_image,
"0x0+0+0");
4156 mse_image=CropImage(mean_image,&geometry,exception);
4157 mean_image=DestroyImage(mean_image);
4158 if (mse_image == (Image *) NULL)
4159 ThrowMSESimilarityException();
4163 (void) ResetImagePage(mse_image,
"0x0+0+0");
4164 (void) ClampImage(mse_image,exception);
4165 mse_image->depth=32;
4166 mse_image->colorspace=GRAYColorspace;
4167 mse_image->alpha_trait=UndefinedPixelTrait;
4168 status=SIMMinimaImage(mse_image,&minima,offset,exception);
4169 if (status == MagickFalse)
4170 ThrowMSESimilarityException();
4171 status=NegateImage(mse_image,MagickFalse,exception);
4172 if (status == MagickFalse)
4173 ThrowMSESimilarityException();
4174 alpha_image=DestroyImage(alpha_image);
4175 beta_image=DestroyImage(beta_image);
4176 if ((QuantumScale*minima) < FLT_EPSILON)
4178 *similarity_metric=QuantumScale*minima;
4182static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
4183 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4185#define ThrowNCCSimilarityException() \
4187 if (alpha_image != (Image *) NULL) \
4188 alpha_image=DestroyImage(alpha_image); \
4189 if (beta_image != (Image *) NULL) \
4190 beta_image=DestroyImage(beta_image); \
4191 if (channel_statistics != (ChannelStatistics *) NULL) \
4192 channel_statistics=(ChannelStatistics *) \
4193 RelinquishMagickMemory(channel_statistics); \
4194 if (correlation_image != (Image *) NULL) \
4195 correlation_image=DestroyImage(correlation_image); \
4196 if (divide_image != (Image *) NULL) \
4197 divide_image=DestroyImage(divide_image); \
4198 if (ncc_image != (Image *) NULL) \
4199 ncc_image=DestroyImage(ncc_image); \
4200 if (normalize_image != (Image *) NULL) \
4201 normalize_image=DestroyImage(normalize_image); \
4202 if (reconstruct_image != (Image *) NULL) \
4203 reconstruct_image=DestroyImage(reconstruct_image); \
4204 if (target_image != (Image *) NULL) \
4205 target_image=DestroyImage(target_image); \
4206 if (variance_image != (Image *) NULL) \
4207 variance_image=DestroyImage(variance_image); \
4208 return((Image *) NULL); \
4212 *channel_statistics = (ChannelStatistics *) NULL;
4218 *alpha_image = (Image *) NULL,
4219 *beta_image = (Image *) NULL,
4220 *correlation_image = (Image *) NULL,
4221 *divide_image = (Image *) NULL,
4222 *ncc_image = (Image *) NULL,
4223 *normalize_image = (Image *) NULL,
4224 *reconstruct_image = (Image *) NULL,
4225 *target_image = (Image *) NULL,
4226 *variance_image = (Image *) NULL;
4229 status = MagickTrue;
4237 target_image=SIMSquareImage(image,exception);
4238 if (target_image == (Image *) NULL)
4239 ThrowNCCSimilarityException();
4240 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4241 if (reconstruct_image == (Image *) NULL)
4242 ThrowNCCSimilarityException();
4246 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4248 target_image=DestroyImage(target_image);
4249 if (alpha_image == (Image *) NULL)
4250 ThrowNCCSimilarityException();
4251 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
4252 reconstruct->columns*reconstruct->rows,(
const ChannelStatistics *) NULL,
4254 if (status == MagickFalse)
4255 ThrowNCCSimilarityException();
4259 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4260 reconstruct_image=DestroyImage(reconstruct_image);
4261 if (beta_image == (Image *) NULL)
4262 ThrowNCCSimilarityException();
4263 target_image=SIMSquareImage(beta_image,exception);
4264 beta_image=DestroyImage(beta_image);
4265 if (target_image == (Image *) NULL)
4266 ThrowNCCSimilarityException();
4267 status=SIMMultiplyImage(target_image,(
double) QuantumRange,
4268 (
const ChannelStatistics *) NULL,exception);
4269 if (status == MagickFalse)
4270 ThrowNCCSimilarityException();
4274 variance_image=SIMVarianceImage(alpha_image,target_image,exception);
4275 target_image=DestroyImage(target_image);
4276 alpha_image=DestroyImage(alpha_image);
4277 if (variance_image == (Image *) NULL)
4278 ThrowNCCSimilarityException();
4282 channel_statistics=GetImageStatistics(reconstruct,exception);
4283 if (channel_statistics == (ChannelStatistics *) NULL)
4284 ThrowNCCSimilarityException();
4285 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
4286 if (status == MagickFalse)
4287 ThrowNCCSimilarityException();
4288 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
4290 channel_statistics=(ChannelStatistics *)
4291 RelinquishMagickMemory(channel_statistics);
4292 if (normalize_image == (Image *) NULL)
4293 ThrowNCCSimilarityException();
4294 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
4295 normalize_image=DestroyImage(normalize_image);
4296 if (correlation_image == (Image *) NULL)
4297 ThrowNCCSimilarityException();
4301 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
4302 correlation_image=DestroyImage(correlation_image);
4303 variance_image=DestroyImage(variance_image);
4304 if (divide_image == (Image *) NULL)
4305 ThrowNCCSimilarityException();
4309 SetGeometry(image,&geometry);
4310 geometry.width=image->columns;
4311 geometry.height=image->rows;
4312 (void) ResetImagePage(divide_image,
"0x0+0+0");
4313 ncc_image=CropImage(divide_image,&geometry,exception);
4314 divide_image=DestroyImage(divide_image);
4315 if (ncc_image == (Image *) NULL)
4316 ThrowNCCSimilarityException();
4320 (void) ResetImagePage(ncc_image,
"0x0+0+0");
4321 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
4322 if (status == MagickFalse)
4323 ThrowNCCSimilarityException();
4324 ncc_image->depth=32;
4325 ncc_image->colorspace=GRAYColorspace;
4326 ncc_image->alpha_trait=UndefinedPixelTrait;
4327 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
4328 if (status == MagickFalse)
4329 ThrowNCCSimilarityException();
4330 if ((QuantumScale*maxima) > 1.0)
4332 status=SIMMultiplyImage(ncc_image,1.0/(QuantumScale*maxima),
4333 (
const ChannelStatistics *) NULL,exception);
4334 maxima=(double) QuantumRange;
4336 *similarity_metric=QuantumScale*maxima;
4340static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
4341 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4343#define ThrowPhaseSimilarityException() \
4345 if (correlation_image != (Image *) NULL) \
4346 correlation_image=DestroyImage(correlation_image); \
4347 if (fft_images != (Image *) NULL) \
4348 fft_images=DestroyImageList(fft_images); \
4349 if (gamma_image != (Image *) NULL) \
4350 gamma_image=DestroyImage(gamma_image); \
4351 if (magnitude_image != (Image *) NULL) \
4352 magnitude_image=DestroyImage(magnitude_image); \
4353 if (phase_image != (Image *) NULL) \
4354 phase_image=DestroyImage(phase_image); \
4355 if (reconstruct_image != (Image *) NULL) \
4356 reconstruct_image=DestroyImage(reconstruct_image); \
4357 if (reconstruct_magnitude != (Image *) NULL) \
4358 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
4359 if (target_image != (Image *) NULL) \
4360 target_image=DestroyImage(target_image); \
4361 if (test_magnitude != (Image *) NULL) \
4362 test_magnitude=DestroyImage(test_magnitude); \
4363 return((Image *) NULL); \
4370 *correlation_image = (Image *) NULL,
4371 *fft_images = (Image *) NULL,
4372 *gamma_image = (Image *) NULL,
4373 *magnitude_image = (Image *) NULL,
4374 *phase_image = (Image *) NULL,
4375 *reconstruct_image = (Image *) NULL,
4376 *reconstruct_magnitude = (Image *) NULL,
4377 *target_image = (Image *) NULL,
4378 *test_magnitude = (Image *) NULL;
4381 status = MagickTrue;
4389 target_image=CloneImage(image,0,0,MagickTrue,exception);
4390 if (target_image == (Image *) NULL)
4391 ThrowPhaseSimilarityException();
4392 (void) ResetImagePage(target_image,
"0x0+0+0");
4393 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4394 &target_image->background_color);
4395 status=SetImageExtent(target_image,2*(
size_t) ceil((
double) image->columns/
4396 2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
4397 if (status == MagickFalse)
4398 ThrowPhaseSimilarityException();
4402 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
4403 if (reconstruct_image == (Image *) NULL)
4404 ThrowPhaseSimilarityException();
4405 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
4406 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4407 &reconstruct_image->background_color);
4408 status=SetImageExtent(reconstruct_image,2*(
size_t) ceil((
double)
4409 image->columns/2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
4410 if (status == MagickFalse)
4411 ThrowPhaseSimilarityException();
4415 (void) SetImageArtifact(target_image,
"fourier:normalize",
"inverse");
4416 fft_images=ForwardFourierTransformImage(target_image,MagickTrue,exception);
4417 if (fft_images == (Image *) NULL)
4418 ThrowPhaseSimilarityException();
4419 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4420 fft_images=DestroyImageList(fft_images);
4421 if (test_magnitude == (Image *) NULL)
4422 ThrowPhaseSimilarityException();
4423 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
4424 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
4426 if (fft_images == (Image *) NULL)
4427 ThrowPhaseSimilarityException();
4428 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4429 fft_images=DestroyImageList(fft_images);
4430 if (reconstruct_magnitude == (Image *) NULL)
4431 ThrowPhaseSimilarityException();
4432 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
4433 if (magnitude_image == (Image *) NULL)
4434 ThrowPhaseSimilarityException();
4435 DisableCompositeClampUnlessSpecified(magnitude_image);
4436 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
4437 MagickTrue,0,0,exception);
4441 correlation_image=SIMPhaseCorrelationImage(target_image,reconstruct_image,
4442 magnitude_image,exception);
4443 target_image=DestroyImage(target_image);
4444 reconstruct_image=DestroyImage(reconstruct_image);
4445 test_magnitude=DestroyImage(test_magnitude);
4446 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
4447 if (correlation_image == (Image *) NULL)
4448 ThrowPhaseSimilarityException();
4452 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
4453 correlation_image=DestroyImage(correlation_image);
4454 if (gamma_image == (Image *) NULL)
4455 ThrowPhaseSimilarityException();
4459 SetGeometry(image,&geometry);
4460 geometry.width=image->columns;
4461 geometry.height=image->rows;
4462 (void) ResetImagePage(gamma_image,
"0x0+0+0");
4463 phase_image=CropImage(gamma_image,&geometry,exception);
4464 gamma_image=DestroyImage(gamma_image);
4465 if (phase_image == (Image *) NULL)
4466 ThrowPhaseSimilarityException();
4467 (void) ResetImagePage(phase_image,
"0x0+0+0");
4471 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
4472 if (status == MagickFalse)
4473 ThrowPhaseSimilarityException();
4474 phase_image->depth=32;
4475 phase_image->colorspace=GRAYColorspace;
4476 phase_image->alpha_trait=UndefinedPixelTrait;
4477 status=SIMFilterImageNaNs(phase_image,exception);
4478 if (status == MagickFalse)
4479 ThrowPhaseSimilarityException();
4480 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
4481 if (status == MagickFalse)
4482 ThrowPhaseSimilarityException();
4483 magnitude_image=DestroyImage(magnitude_image);
4484 if ((QuantumScale*maxima) > 1.0)
4486 status=SIMMultiplyImage(phase_image,1.0/(QuantumScale*maxima),
4487 (
const ChannelStatistics *) NULL,exception);
4488 maxima=(double) QuantumRange;
4490 *similarity_metric=QuantumScale*maxima;
4491 return(phase_image);
4494static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
4495 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4498 *psnr_image = (Image *) NULL;
4500 psnr_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4502 if (psnr_image == (Image *) NULL)
4504 *similarity_metric=10.0*MagickSafeLog10(MagickSafeReciprocal(
4505 *similarity_metric))/MagickSafePSNRRecipicol(10.0);
4509static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
4510 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4513 *rmse_image = (Image *) NULL;
4515 rmse_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4517 if (rmse_image == (Image *) NULL)
4519 *similarity_metric=sqrt(*similarity_metric);
4524static double GetSimilarityMetric(
const Image *image,
4525 const Image *reconstruct_image,
const MetricType metric,
4526 const ssize_t x_offset,
const ssize_t y_offset,ExceptionInfo *exception)
4529 *channel_similarity,
4533 *sans_exception = AcquireExceptionInfo();
4539 status = MagickTrue;
4545 length = MaxPixelChannels+1UL;
4547 SetGeometry(reconstruct_image,&geometry);
4548 geometry.x=x_offset;
4549 geometry.y=y_offset;
4550 similarity_image=CropImage(image,&geometry,sans_exception);
4551 sans_exception=DestroyExceptionInfo(sans_exception);
4552 if (similarity_image == (Image *) NULL)
4557 channel_similarity=(
double *) AcquireQuantumMemory(length,
4558 sizeof(*channel_similarity));
4559 if (channel_similarity == (
double *) NULL)
4560 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4561 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
4564 case AbsoluteErrorMetric:
4566 status=GetAESimilarity(similarity_image,reconstruct_image,
4567 channel_similarity,exception);
4570 case DotProductCorrelationErrorMetric:
4572 status=GetDPCSimilarity(similarity_image,reconstruct_image,
4573 channel_similarity,exception);
4576 case FuzzErrorMetric:
4578 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
4579 channel_similarity,exception);
4582 case MeanAbsoluteErrorMetric:
4584 status=GetMAESimilarity(similarity_image,reconstruct_image,
4585 channel_similarity,exception);
4588 case MeanErrorPerPixelErrorMetric:
4590 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
4591 channel_similarity,exception);
4594 case MeanSquaredErrorMetric:
4596 status=GetMSESimilarity(similarity_image,reconstruct_image,
4597 channel_similarity,exception);
4600 case NormalizedCrossCorrelationErrorMetric:
4602 status=GetNCCSimilarity(similarity_image,reconstruct_image,
4603 channel_similarity,exception);
4606 case PeakAbsoluteErrorMetric:
4608 status=GetPASimilarity(similarity_image,reconstruct_image,
4609 channel_similarity,exception);
4612 case PeakSignalToNoiseRatioErrorMetric:
4614 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
4615 channel_similarity,exception);
4618 case PerceptualHashErrorMetric:
4620 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
4621 channel_similarity,exception);
4624 case PhaseCorrelationErrorMetric:
4626 status=GetPHASESimilarity(similarity_image,reconstruct_image,
4627 channel_similarity,exception);
4630 case PixelDifferenceCountErrorMetric:
4632 status=GetPDCSimilarity(similarity_image,reconstruct_image,
4633 channel_similarity,exception);
4636 case RootMeanSquaredErrorMetric:
4637 case UndefinedErrorMetric:
4640 status=GetRMSESimilarity(similarity_image,reconstruct_image,
4641 channel_similarity,exception);
4644 case StructuralDissimilarityErrorMetric:
4646 status=GetDSSIMSimilarity(similarity_image,reconstruct_image,
4647 channel_similarity,exception);
4650 case StructuralSimilarityErrorMetric:
4652 status=GetSSIMSimularity(similarity_image,reconstruct_image,
4653 channel_similarity,exception);
4657 similarity_image=DestroyImage(similarity_image);
4658 similarity=channel_similarity[CompositePixelChannel];
4659 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
4660 if (status == MagickFalse)
4665MagickExport Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4666 const MetricType metric,
const double similarity_threshold,
4667 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4669#define SimilarityImageTag "Similarity/Image"
4685 *similarity_image = (Image *) NULL;
4688 status = MagickTrue;
4694 similarity_info = { 0.0, 0, 0 };
4699 assert(image != (
const Image *) NULL);
4700 assert(image->signature == MagickCoreSignature);
4701 assert(exception != (ExceptionInfo *) NULL);
4702 assert(exception->signature == MagickCoreSignature);
4703 assert(offset != (RectangleInfo *) NULL);
4704 if (IsEventLogging() != MagickFalse)
4705 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4706 SetGeometry(reconstruct,offset);
4707 *similarity_metric=0.0;
4710#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4712 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4713 if (artifact == (
const char *) NULL)
4714 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4715 if (((artifact == (
const char *) NULL) ||
4716 (IsStringTrue(artifact) != MagickFalse)) &&
4717 ((image->channels & ReadMaskChannel) == 0))
4720 case DotProductCorrelationErrorMetric:
4722 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4723 similarity_metric,exception);
4724 return(similarity_image);
4726 case MeanSquaredErrorMetric:
4728 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4729 similarity_metric,exception);
4730 return(similarity_image);
4732 case NormalizedCrossCorrelationErrorMetric:
4734 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4735 similarity_metric,exception);
4736 return(similarity_image);
4738 case PeakSignalToNoiseRatioErrorMetric:
4740 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4741 similarity_metric,exception);
4742 return(similarity_image);
4744 case PhaseCorrelationErrorMetric:
4746 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4747 similarity_metric,exception);
4748 return(similarity_image);
4750 case RootMeanSquaredErrorMetric:
4751 case UndefinedErrorMetric:
4753 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4754 similarity_metric,exception);
4755 return(similarity_image);
4762 if ((image->columns < reconstruct->columns) ||
4763 (image->rows < reconstruct->rows))
4765 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4766 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4767 return((Image *) NULL);
4769 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4771 if (similarity_image == (Image *) NULL)
4772 return((Image *) NULL);
4773 similarity_image->depth=32;
4774 similarity_image->colorspace=GRAYColorspace;
4775 similarity_image->alpha_trait=UndefinedPixelTrait;
4776 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4777 if (status == MagickFalse)
4778 return(DestroyImage(similarity_image));
4782 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
4783 similarity_info.x,similarity_info.y,exception);
4784 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4785#if defined(MAGICKCORE_OPENMP_SUPPORT)
4786 #pragma omp parallel for schedule(static) shared(similarity_info,status) \
4787 magick_number_threads(image,reconstruct,similarity_image->rows,1)
4789 for (y=0; y < (ssize_t) similarity_image->rows; y++)
4795 threshold_trigger = MagickFalse;
4801 channel_info = similarity_info;
4806 if (status == MagickFalse)
4808 if (threshold_trigger != MagickFalse)
4810 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4811 similarity_image->columns,1,exception);
4812 if (q == (Quantum *) NULL)
4817 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4822 similarity=GetSimilarityMetric((Image *) image,reconstruct,metric,x,y,
4826 case DotProductCorrelationErrorMetric:
4827 case NormalizedCrossCorrelationErrorMetric:
4828 case PeakSignalToNoiseRatioErrorMetric:
4829 case PhaseCorrelationErrorMetric:
4830 case StructuralSimilarityErrorMetric:
4832 if (similarity <= channel_info.similarity)
4834 channel_info.similarity=similarity;
4841 if (similarity >= channel_info.similarity)
4843 channel_info.similarity=similarity;
4849 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4851 PixelChannel channel = GetPixelChannelChannel(image,i);
4852 PixelTrait traits = GetPixelChannelTraits(image,channel);
4853 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4855 if (((traits & UpdatePixelTrait) == 0) ||
4856 ((similarity_traits & UpdatePixelTrait) == 0))
4860 case DotProductCorrelationErrorMetric:
4861 case NormalizedCrossCorrelationErrorMetric:
4862 case PeakSignalToNoiseRatioErrorMetric:
4863 case PhaseCorrelationErrorMetric:
4864 case StructuralSimilarityErrorMetric:
4866 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4867 QuantumRange*similarity),q);
4872 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4873 QuantumRange*(1.0-similarity)),q);
4878 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4880#if defined(MAGICKCORE_OPENMP_SUPPORT)
4881 #pragma omp critical (MagickCore_GetSimilarityMetric)
4885 case DotProductCorrelationErrorMetric:
4886 case NormalizedCrossCorrelationErrorMetric:
4887 case PeakSignalToNoiseRatioErrorMetric:
4888 case PhaseCorrelationErrorMetric:
4889 case StructuralSimilarityErrorMetric:
4891 if (similarity_threshold != DefaultSimilarityThreshold)
4892 if (channel_info.similarity >= similarity_threshold)
4893 threshold_trigger=MagickTrue;
4894 if (channel_info.similarity >= similarity_info.similarity)
4895 similarity_info=channel_info;
4900 if (similarity_threshold != DefaultSimilarityThreshold)
4901 if (channel_info.similarity < similarity_threshold)
4902 threshold_trigger=MagickTrue;
4903 if (channel_info.similarity < similarity_info.similarity)
4904 similarity_info=channel_info;
4908 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4910 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4916 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4917 if (proceed == MagickFalse)
4921 similarity_view=DestroyCacheView(similarity_view);
4922 if (status == MagickFalse)
4923 similarity_image=DestroyImage(similarity_image);
4924 *similarity_metric=similarity_info.similarity;
4925 if (fabs(*similarity_metric) < MagickEpsilon)
4926 *similarity_metric=0.0;
4927 offset->x=similarity_info.x;
4928 offset->y=similarity_info.y;
4929 (void) FormatImageProperty((Image *) image,
"similarity",
"%.*g",
4930 GetMagickPrecision(),*similarity_metric);
4931 (void) FormatImageProperty((Image *) image,
"similarity.offset.x",
"%.*g",
4932 GetMagickPrecision(),(
double) offset->x);
4933 (void) FormatImageProperty((Image *) image,
"similarity.offset.y",
"%.*g",
4934 GetMagickPrecision(),(
double) offset->y);
4935 return(similarity_image);