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 DFTPhaseSpectrum(
const Image *image,
const ssize_t u,
1527 const ssize_t v,
double *phase,ExceptionInfo *exception)
1529#define PhaseImageTag "Phase/Image"
1535 channel_imag[MaxPixelChannels+1] = { 0.0 },
1536 channel_real[MaxPixelChannels+1] = { 0.0 };
1549 image_view=AcquireVirtualCacheView(image,exception);
1550 for (y=0; y < (ssize_t) image->rows; y++)
1558 if (status == MagickFalse)
1560 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1561 if (p == (
const Quantum *) NULL)
1566 for (x=0; x < (ssize_t) image->columns; x++)
1575 angle=MagickPI*((u*x/(double) image->rows)+(v*y/(double) image->columns));
1576 Sa=QuantumScale*(double) GetPixelAlpha(image,p);
1577 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1579 PixelChannel channel = GetPixelChannelChannel(image,i);
1580 PixelTrait traits = GetPixelChannelTraits(image,channel);
1581 if (traits == UndefinedPixelTrait)
1583 if (channel == AlphaPixelChannel)
1585 channel_real[i]+=(QuantumScale*p[i])*cos(angle);
1586 channel_imag[i]-=(QuantumScale*p[i])*sin(angle);
1590 channel_real[i]+=(QuantumScale*Sa*p[i])*cos(angle);
1591 channel_imag[i]-=(QuantumScale*Sa*p[i])*sin(angle);
1594 p+=(ptrdiff_t) GetPixelChannels(image);
1597 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1598 phase[k]=atan2(channel_imag[k],channel_real[k]);
1599 phase[CompositePixelChannel]=atan2(channel_imag[CompositePixelChannel],
1600 channel_real[CompositePixelChannel]);
1601 image_view=DestroyCacheView(image_view);
1605static MagickBooleanType GetPHASESimilarity(
const Image *image,
1606 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1616 status = MagickTrue;
1629 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1630 image_view=AcquireVirtualCacheView(image,exception);
1631 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1632#if defined(MAGICKCORE_OPENMP_SUPPORT)
1633 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1634 magick_number_threads(image,image,rows,1)
1636 for (y=0; y < (ssize_t) rows; y++)
1644 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1649 if (status == MagickFalse)
1651 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
1652 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
1653 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
1658 for (x=0; x < (ssize_t) columns; x++)
1661 phase[MaxPixelChannels+1] = { 0.0 },
1662 reconstruct_phase[MaxPixelChannels+1] = { 0.0 };
1667 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
1668 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
1670 p+=(ptrdiff_t) GetPixelChannels(image);
1671 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1674 status=DFTPhaseSpectrum(image,x,y,phase,exception);
1675 if (status == MagickFalse)
1677 status=DFTPhaseSpectrum(reconstruct_image,x,y,reconstruct_phase,
1679 if (status == MagickFalse)
1681 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1686 PixelChannel channel = GetPixelChannelChannel(image,i);
1687 PixelTrait traits = GetPixelChannelTraits(image,channel);
1688 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1690 if (((traits & UpdatePixelTrait) == 0) ||
1691 ((reconstruct_traits & UpdatePixelTrait) == 0))
1693 delta=phase[i]-reconstruct_phase[i];
1694 channel_similarity[i]+=cos(delta);
1695 channel_similarity[CompositePixelChannel]+=cos(delta);
1698 p+=(ptrdiff_t) GetPixelChannels(image);
1699 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
1701#if defined(MAGICKCORE_OPENMP_SUPPORT)
1702 #pragma omp critical (MagickCore_GetPHASESimilarity)
1709 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1711 PixelChannel channel = GetPixelChannelChannel(image,j);
1712 PixelTrait traits = GetPixelChannelTraits(image,channel);
1713 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1715 if (((traits & UpdatePixelTrait) == 0) ||
1716 ((reconstruct_traits & UpdatePixelTrait) == 0))
1718 similarity[j]+=channel_similarity[j];
1720 similarity[CompositePixelChannel]+=
1721 channel_similarity[CompositePixelChannel];
1724 reconstruct_view=DestroyCacheView(reconstruct_view);
1725 image_view=DestroyCacheView(image_view);
1726 area=MagickSafeReciprocal(area);
1727 for (k=0; k < (ssize_t) GetPixelChannels(image); k++)
1729 PixelChannel channel = GetPixelChannelChannel(image,k);
1730 PixelTrait traits = GetPixelChannelTraits(image,channel);
1731 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1733 if (((traits & UpdatePixelTrait) == 0) ||
1734 ((reconstruct_traits & UpdatePixelTrait) == 0))
1736 similarity[k]*=area;
1738 similarity[CompositePixelChannel]*=area;
1739 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1743static MagickBooleanType GetPSNRSimilarity(
const Image *image,
1744 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1747 status = MagickTrue;
1755 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
1756 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1758 PixelChannel channel = GetPixelChannelChannel(image,i);
1759 PixelTrait traits = GetPixelChannelTraits(image,channel);
1760 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1762 if (((traits & UpdatePixelTrait) == 0) ||
1763 ((reconstruct_traits & UpdatePixelTrait) == 0))
1765 similarity[i]=10.0*MagickSafeLog10(MagickSafeReciprocal(
1766 similarity[i]))/MagickSafePSNRRecipicol(10.0);
1768 similarity[CompositePixelChannel]=10.0*MagickSafeLog10(
1769 MagickSafeReciprocal(similarity[CompositePixelChannel]))/
1770 MagickSafePSNRRecipicol(10.0);
1774static MagickBooleanType GetPHASHSimilarity(
const Image *image,
1775 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1777#define PHASHNormalizationFactor 389.373723242
1779 ChannelPerceptualHash
1792 channel_phash=GetImagePerceptualHash(image,exception);
1793 if (channel_phash == (ChannelPerceptualHash *) NULL)
1794 return(MagickFalse);
1795 reconstruct_phash=GetImagePerceptualHash(reconstruct_image,exception);
1796 if (reconstruct_phash == (ChannelPerceptualHash *) NULL)
1798 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1800 return(MagickFalse);
1802 for (k=0; k < MaxPixelChannels; k++)
1810 PixelChannel channel = GetPixelChannelChannel(image,k);
1811 PixelTrait traits = GetPixelChannelTraits(image,channel);
1812 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1814 if (((traits & UpdatePixelTrait) == 0) ||
1815 ((reconstruct_traits & UpdatePixelTrait) == 0))
1818 for (i=0; i < MaximumNumberOfImageMoments; i++)
1827 for (j=0; j < (ssize_t) channel_phash[0].number_colorspaces; j++)
1832 alpha=channel_phash[k].phash[j][i];
1833 beta=reconstruct_phash[k].phash[j][i];
1835 if (IsNaN(error) != 0)
1837 difference+=error*error/PHASHNormalizationFactor;
1840 similarity[k]+=difference;
1841 similarity[CompositePixelChannel]+=difference;
1843 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
1844 artifact=GetImageArtifact(image,
"phash:normalize");
1845 if (IsStringTrue(artifact) != MagickFalse)
1850 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
1852 PixelChannel channel = GetPixelChannelChannel(image,j);
1853 PixelTrait traits = GetPixelChannelTraits(image,channel);
1854 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1856 if (((traits & UpdatePixelTrait) == 0) ||
1857 ((reconstruct_traits & UpdatePixelTrait) == 0))
1859 similarity[j]=sqrt(similarity[j]/channel_phash[0].number_colorspaces);
1861 similarity[CompositePixelChannel]=sqrt(similarity[CompositePixelChannel]/
1862 channel_phash[0].number_colorspaces);
1867 reconstruct_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(
1869 channel_phash=(ChannelPerceptualHash *) RelinquishMagickMemory(channel_phash);
1873static MagickBooleanType GetRMSESimilarity(
const Image *image,
1874 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1876#define RMSESquareRoot(x) sqrt((x) < 0.0 ? 0.0 : (x))
1879 status = MagickTrue;
1887 status=GetMSESimilarity(image,reconstruct_image,similarity,exception);
1888 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
1890 PixelChannel channel = GetPixelChannelChannel(image,i);
1891 PixelTrait traits = GetPixelChannelTraits(image,channel);
1892 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
1894 if (((traits & UpdatePixelTrait) == 0) ||
1895 ((reconstruct_traits & UpdatePixelTrait) == 0))
1897 similarity[i]=RMSESquareRoot(similarity[i]);
1899 similarity[CompositePixelChannel]=RMSESquareRoot(
1900 similarity[CompositePixelChannel]);
1904static MagickBooleanType GetSSIMSimularity(
const Image *image,
1905 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
1907#define SSIMRadius 5.0
1908#define SSIMSigma 1.5
1918 geometry[MagickPathExtent];
1934 status = MagickTrue;
1948 artifact=GetImageArtifact(image,
"compare:ssim-radius");
1949 if (artifact != (
const char *) NULL)
1950 radius=StringToDouble(artifact,(
char **) NULL);
1952 artifact=GetImageArtifact(image,
"compare:ssim-sigma");
1953 if (artifact != (
const char *) NULL)
1954 sigma=StringToDouble(artifact,(
char **) NULL);
1955 (void) FormatLocaleString(geometry,MagickPathExtent,
"gaussian:%.20gx%.20g",
1957 kernel_info=AcquireKernelInfo(geometry,exception);
1958 if (kernel_info == (KernelInfo *) NULL)
1959 ThrowBinaryException(ResourceLimitError,
"MemoryAllocationFailed",
1961 c1=pow(SSIMK1*SSIML,2.0);
1962 artifact=GetImageArtifact(image,
"compare:ssim-k1");
1963 if (artifact != (
const char *) NULL)
1964 c1=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1965 c2=pow(SSIMK2*SSIML,2.0);
1966 artifact=GetImageArtifact(image,
"compare:ssim-k2");
1967 if (artifact != (
const char *) NULL)
1968 c2=pow(StringToDouble(artifact,(
char **) NULL)*SSIML,2.0);
1969 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
1970 image_view=AcquireVirtualCacheView(image,exception);
1971 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
1972#if defined(MAGICKCORE_OPENMP_SUPPORT)
1973 #pragma omp parallel for schedule(static) shared(area,similarity,status) \
1974 magick_number_threads(image,reconstruct_image,rows,1)
1976 for (y=0; y < (ssize_t) rows; y++)
1984 channel_similarity[MaxPixelChannels+1] = { 0.0 };
1990 if (status == MagickFalse)
1992 p=GetCacheViewVirtualPixels(image_view,-((ssize_t) kernel_info->width/2L),y-
1993 ((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1994 kernel_info->height,exception);
1995 q=GetCacheViewVirtualPixels(reconstruct_view,-((ssize_t) kernel_info->width/
1996 2L),y-((ssize_t) kernel_info->height/2L),columns+kernel_info->width,
1997 kernel_info->height,exception);
1998 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2003 for (x=0; x < (ssize_t) columns; x++)
2006 *magick_restrict reconstruct,
2007 *magick_restrict test;
2010 x_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2011 x_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 },
2012 xy_sigma[MaxPixelChannels+1] = { 0.0 },
2013 y_pixel_mu[MaxPixelChannels+1] = { 0.0 },
2014 y_pixel_sigma_squared[MaxPixelChannels+1] = { 0.0 };
2022 if ((GetPixelReadMask(image,p) <= (QuantumRange/2)) ||
2023 (GetPixelReadMask(reconstruct_image,q) <= (QuantumRange/2)))
2025 p+=(ptrdiff_t) GetPixelChannels(image);
2026 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2029 k=kernel_info->values;
2032 for (v=0; v < (ssize_t) kernel_info->height; v++)
2037 for (u=0; u < (ssize_t) kernel_info->width; u++)
2039 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2045 PixelChannel channel = GetPixelChannelChannel(image,i);
2046 PixelTrait traits = GetPixelChannelTraits(image,channel);
2047 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2048 reconstruct_image,channel);
2049 if (((traits & UpdatePixelTrait) == 0) ||
2050 ((reconstruct_traits & UpdatePixelTrait) == 0))
2052 x_pixel=QuantumScale*(double) test[i];
2053 x_pixel_mu[i]+=(*k)*x_pixel;
2054 x_pixel_sigma_squared[i]+=(*k)*x_pixel*x_pixel;
2055 y_pixel=QuantumScale*(double)
2056 GetPixelChannel(reconstruct_image,channel,reconstruct);
2057 y_pixel_mu[i]+=(*k)*y_pixel;
2058 y_pixel_sigma_squared[i]+=(*k)*y_pixel*y_pixel;
2059 xy_sigma[i]+=(*k)*x_pixel*y_pixel;
2062 test+=(ptrdiff_t) GetPixelChannels(image);
2063 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2065 test+=(ptrdiff_t) GetPixelChannels(image)*columns;
2066 reconstruct+=(ptrdiff_t) GetPixelChannels(reconstruct_image)*columns;
2068 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2073 x_pixel_sigmas_squared,
2077 y_pixel_sigmas_squared;
2079 PixelChannel channel = GetPixelChannelChannel(image,i);
2080 PixelTrait traits = GetPixelChannelTraits(image,channel);
2081 PixelTrait reconstruct_traits = GetPixelChannelTraits(
2082 reconstruct_image,channel);
2083 if (((traits & UpdatePixelTrait) == 0) ||
2084 ((reconstruct_traits & UpdatePixelTrait) == 0))
2086 x_pixel_mu_squared=x_pixel_mu[i]*x_pixel_mu[i];
2087 y_pixel_mu_squared=y_pixel_mu[i]*y_pixel_mu[i];
2088 xy_mu=x_pixel_mu[i]*y_pixel_mu[i];
2089 xy_sigmas=xy_sigma[i]-xy_mu;
2090 x_pixel_sigmas_squared=x_pixel_sigma_squared[i]-x_pixel_mu_squared;
2091 y_pixel_sigmas_squared=y_pixel_sigma_squared[i]-y_pixel_mu_squared;
2092 ssim=((2.0*xy_mu+c1)*(2.0*xy_sigmas+c2))*
2093 MagickSafeReciprocal((x_pixel_mu_squared+y_pixel_mu_squared+c1)*
2094 (x_pixel_sigmas_squared+y_pixel_sigmas_squared+c2));
2095 channel_similarity[i]+=ssim;
2096 channel_similarity[CompositePixelChannel]+=ssim;
2098 p+=(ptrdiff_t) GetPixelChannels(image);
2099 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2102#if defined(MAGICKCORE_OPENMP_SUPPORT)
2103 #pragma omp critical (MagickCore_GetSSIMSimularity)
2110 for (j=0; j < (ssize_t) GetPixelChannels(image); j++)
2112 PixelChannel channel = GetPixelChannelChannel(image,j);
2113 PixelTrait traits = GetPixelChannelTraits(image,channel);
2114 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2116 if (((traits & UpdatePixelTrait) == 0) ||
2117 ((reconstruct_traits & UpdatePixelTrait) == 0))
2119 similarity[j]+=channel_similarity[j];
2121 similarity[CompositePixelChannel]+=
2122 channel_similarity[CompositePixelChannel];
2125 image_view=DestroyCacheView(image_view);
2126 reconstruct_view=DestroyCacheView(reconstruct_view);
2127 area=MagickSafeReciprocal(area);
2128 for (l=0; l < (ssize_t) GetPixelChannels(image); l++)
2130 PixelChannel channel = GetPixelChannelChannel(image,l);
2131 PixelTrait traits = GetPixelChannelTraits(image,channel);
2132 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2134 if (((traits & UpdatePixelTrait) == 0) ||
2135 ((reconstruct_traits & UpdatePixelTrait) == 0))
2137 similarity[l]*=area;
2139 similarity[CompositePixelChannel]*=area;
2140 similarity[CompositePixelChannel]/=(double) GetImageChannels(image);
2141 kernel_info=DestroyKernelInfo(kernel_info);
2145static MagickBooleanType GetDSSIMSimilarity(
const Image *image,
2146 const Image *reconstruct_image,
double *similarity,ExceptionInfo *exception)
2149 status = MagickTrue;
2157 status=GetSSIMSimularity(image,reconstruct_image,similarity,exception);
2158 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2160 PixelChannel channel = GetPixelChannelChannel(image,i);
2161 PixelTrait traits = GetPixelChannelTraits(image,channel);
2162 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2164 if (((traits & UpdatePixelTrait) == 0) ||
2165 ((reconstruct_traits & UpdatePixelTrait) == 0))
2167 similarity[i]=(1.0-similarity[i])/2.0;
2169 similarity[CompositePixelChannel]=(1.0-similarity[CompositePixelChannel])/2.0;
2173MagickExport MagickBooleanType GetImageDistortion(Image *image,
2174 const Image *reconstruct_image,
const MetricType metric,
double *distortion,
2175 ExceptionInfo *exception)
2177#define CompareMetricNotSupportedException "metric not supported"
2180 *channel_similarity;
2183 status = MagickTrue;
2188 assert(image != (Image *) NULL);
2189 assert(image->signature == MagickCoreSignature);
2190 assert(reconstruct_image != (
const Image *) NULL);
2191 assert(reconstruct_image->signature == MagickCoreSignature);
2192 assert(distortion != (
double *) NULL);
2193 if (IsEventLogging() != MagickFalse)
2194 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2199 length=MaxPixelChannels+1UL;
2200 channel_similarity=(
double *) AcquireQuantumMemory(length,
2201 sizeof(*channel_similarity));
2202 if (channel_similarity == (
double *) NULL)
2203 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2204 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2207 case AbsoluteErrorMetric:
2209 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2213 case DotProductCorrelationErrorMetric:
2215 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2219 case FuzzErrorMetric:
2221 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2225 case MeanAbsoluteErrorMetric:
2227 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2231 case MeanErrorPerPixelErrorMetric:
2233 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2237 case MeanSquaredErrorMetric:
2239 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2243 case NormalizedCrossCorrelationErrorMetric:
2245 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2249 case PeakAbsoluteErrorMetric:
2251 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2255 case PeakSignalToNoiseRatioErrorMetric:
2257 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2261 case PerceptualHashErrorMetric:
2263 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2267 case PhaseCorrelationErrorMetric:
2269 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2273 case RootMeanSquaredErrorMetric:
2274 case UndefinedErrorMetric:
2277 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2281 case StructuralDissimilarityErrorMetric:
2283 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2287 case StructuralSimilarityErrorMetric:
2289 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2294 *distortion=channel_similarity[CompositePixelChannel];
2297 case DotProductCorrelationErrorMetric:
2298 case NormalizedCrossCorrelationErrorMetric:
2299 case PhaseCorrelationErrorMetric:
2300 case StructuralSimilarityErrorMetric:
2302 *distortion=(1.0-(*distortion))/2.0;
2307 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2308 if (fabs(*distortion) < MagickEpsilon)
2310 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2346MagickExport
double *GetImageDistortions(Image *image,
2347 const Image *reconstruct_image,
const MetricType metric,
2348 ExceptionInfo *exception)
2352 *channel_similarity;
2355 status = MagickTrue;
2363 assert(image != (Image *) NULL);
2364 assert(image->signature == MagickCoreSignature);
2365 assert(reconstruct_image != (
const Image *) NULL);
2366 assert(reconstruct_image->signature == MagickCoreSignature);
2367 if (IsEventLogging() != MagickFalse)
2368 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2372 length=MaxPixelChannels+1UL;
2373 channel_similarity=(
double *) AcquireQuantumMemory(length,
2374 sizeof(*channel_similarity));
2375 if (channel_similarity == (
double *) NULL)
2376 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
2377 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
2380 case AbsoluteErrorMetric:
2382 status=GetAESimilarity(image,reconstruct_image,channel_similarity,
2386 case DotProductCorrelationErrorMetric:
2388 status=GetDPCSimilarity(image,reconstruct_image,channel_similarity,
2392 case FuzzErrorMetric:
2394 status=GetFUZZSimilarity(image,reconstruct_image,channel_similarity,
2398 case MeanAbsoluteErrorMetric:
2400 status=GetMAESimilarity(image,reconstruct_image,channel_similarity,
2404 case MeanErrorPerPixelErrorMetric:
2406 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2410 case MeanSquaredErrorMetric:
2412 status=GetMSESimilarity(image,reconstruct_image,channel_similarity,
2416 case NormalizedCrossCorrelationErrorMetric:
2418 status=GetNCCSimilarity(image,reconstruct_image,channel_similarity,
2422 case PeakAbsoluteErrorMetric:
2424 status=GetPASimilarity(image,reconstruct_image,channel_similarity,
2428 case PeakSignalToNoiseRatioErrorMetric:
2430 status=GetPSNRSimilarity(image,reconstruct_image,channel_similarity,
2434 case PerceptualHashErrorMetric:
2436 status=GetPHASHSimilarity(image,reconstruct_image,channel_similarity,
2440 case PhaseCorrelationErrorMetric:
2442 status=GetPHASESimilarity(image,reconstruct_image,channel_similarity,
2446 case RootMeanSquaredErrorMetric:
2447 case UndefinedErrorMetric:
2450 status=GetRMSESimilarity(image,reconstruct_image,channel_similarity,
2454 case StructuralDissimilarityErrorMetric:
2456 status=GetDSSIMSimilarity(image,reconstruct_image,channel_similarity,
2460 case StructuralSimilarityErrorMetric:
2462 status=GetSSIMSimularity(image,reconstruct_image,channel_similarity,
2467 if (status == MagickFalse)
2469 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
2470 return((
double *) NULL);
2472 distortion=channel_similarity;
2475 case DotProductCorrelationErrorMetric:
2476 case NormalizedCrossCorrelationErrorMetric:
2477 case PhaseCorrelationErrorMetric:
2478 case StructuralSimilarityErrorMetric:
2480 for (i=0; i <= MaxPixelChannels; i++)
2481 distortion[i]=(1.0-distortion[i])/2.0;
2486 for (i=0; i <= MaxPixelChannels; i++)
2487 if (fabs(distortion[i]) < MagickEpsilon)
2489 (void) FormatImageProperty(image,
"distortion",
"%.*g",GetMagickPrecision(),
2490 distortion[CompositePixelChannel]);
2522MagickExport MagickBooleanType IsImagesEqual(
const Image *image,
2523 const Image *reconstruct_image,ExceptionInfo *exception)
2536 assert(image != (Image *) NULL);
2537 assert(image->signature == MagickCoreSignature);
2538 assert(reconstruct_image != (
const Image *) NULL);
2539 assert(reconstruct_image->signature == MagickCoreSignature);
2540 SetImageCompareBounds(image,reconstruct_image,&columns,&rows);
2541 image_view=AcquireVirtualCacheView(image,exception);
2542 reconstruct_view=AcquireVirtualCacheView(reconstruct_image,exception);
2543 for (y=0; y < (ssize_t) rows; y++)
2552 p=GetCacheViewVirtualPixels(image_view,0,y,columns,1,exception);
2553 q=GetCacheViewVirtualPixels(reconstruct_view,0,y,columns,1,exception);
2554 if ((p == (
const Quantum *) NULL) || (q == (
const Quantum *) NULL))
2556 for (x=0; x < (ssize_t) columns; x++)
2561 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2566 PixelChannel channel = GetPixelChannelChannel(image,i);
2567 PixelTrait traits = GetPixelChannelTraits(image,channel);
2568 PixelTrait reconstruct_traits = GetPixelChannelTraits(reconstruct_image,
2570 if (((traits & UpdatePixelTrait) == 0) ||
2571 ((reconstruct_traits & UpdatePixelTrait) == 0))
2573 distance=fabs((
double) p[i]-(
double) GetPixelChannel(reconstruct_image,
2575 if (distance >= MagickEpsilon)
2578 if (i < (ssize_t) GetPixelChannels(image))
2580 p+=(ptrdiff_t) GetPixelChannels(image);
2581 q+=(ptrdiff_t) GetPixelChannels(reconstruct_image);
2583 if (x < (ssize_t) columns)
2586 reconstruct_view=DestroyCacheView(reconstruct_view);
2587 image_view=DestroyCacheView(image_view);
2588 return(y < (ssize_t) rows ? MagickFalse : MagickTrue);
2640MagickExport MagickBooleanType SetImageColorMetric(Image *image,
2641 const Image *reconstruct_image,ExceptionInfo *exception)
2644 channel_similarity[MaxPixelChannels+1] = { 0.0 };
2649 status=GetMEPPSimilarity(image,reconstruct_image,channel_similarity,
2651 if (status == MagickFalse)
2652 return(MagickFalse);
2653 status=fabs(image->error.mean_error_per_pixel) < MagickEpsilon ?
2654 MagickTrue : MagickFalse;
2701#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
2702static Image *SIMCrossCorrelationImage(
const Image *alpha_image,
2703 const Image *beta_image,ExceptionInfo *exception)
2706 *alpha_fft = (Image *) NULL,
2707 *beta_fft = (Image *) NULL,
2708 *complex_conjugate = (Image *) NULL,
2709 *complex_multiplication = (Image *) NULL,
2710 *cross_correlation = (Image *) NULL,
2711 *temp_image = (Image *) NULL;
2716 temp_image=CloneImage(beta_image,0,0,MagickTrue,exception);
2717 if (temp_image == (Image *) NULL)
2718 return((Image *) NULL);
2719 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2720 beta_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2721 temp_image=DestroyImageList(temp_image);
2722 if (beta_fft == (Image *) NULL)
2723 return((Image *) NULL);
2727 complex_conjugate=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
2728 beta_fft=DestroyImageList(beta_fft);
2729 if (complex_conjugate == (Image *) NULL)
2730 return((Image *) NULL);
2734 temp_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
2735 if (temp_image == (Image *) NULL)
2737 complex_conjugate=DestroyImageList(complex_conjugate);
2738 return((Image *) NULL);
2740 (void) SetImageArtifact(temp_image,
"fourier:normalize",
"inverse");
2741 alpha_fft=ForwardFourierTransformImage(temp_image,MagickFalse,exception);
2742 temp_image=DestroyImageList(temp_image);
2743 if (alpha_fft == (Image *) NULL)
2745 complex_conjugate=DestroyImageList(complex_conjugate);
2746 return((Image *) NULL);
2751 DisableCompositeClampUnlessSpecified(complex_conjugate);
2752 DisableCompositeClampUnlessSpecified(complex_conjugate->next);
2753 AppendImageToList(&complex_conjugate,alpha_fft);
2754 complex_multiplication=ComplexImages(complex_conjugate,
2755 MultiplyComplexOperator,exception);
2756 complex_conjugate=DestroyImageList(complex_conjugate);
2757 if (complex_multiplication == (Image *) NULL)
2758 return((Image *) NULL);
2762 cross_correlation=InverseFourierTransformImage(complex_multiplication,
2763 complex_multiplication->next,MagickFalse,exception);
2764 complex_multiplication=DestroyImageList(complex_multiplication);
2765 return(cross_correlation);
2768static Image *SIMDerivativeImage(
const Image *image,
const char *kernel,
2769 ExceptionInfo *exception)
2777 kernel_info=AcquireKernelInfo(kernel,exception);
2778 if (kernel_info == (KernelInfo *) NULL)
2779 return((Image *) NULL);
2780 derivative_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,
2782 kernel_info=DestroyKernelInfo(kernel_info);
2783 return(derivative_image);
2786static Image *SIMDivideImage(
const Image *numerator_image,
2787 const Image *denominator_image,ExceptionInfo *exception)
2797 status = MagickTrue;
2805 divide_image=CloneImage(numerator_image,0,0,MagickTrue,exception);
2806 if (divide_image == (Image *) NULL)
2807 return(divide_image);
2808 numerator_view=AcquireAuthenticCacheView(divide_image,exception);
2809 denominator_view=AcquireVirtualCacheView(denominator_image,exception);
2810#if defined(MAGICKCORE_OPENMP_SUPPORT)
2811 #pragma omp parallel for schedule(static) shared(status) \
2812 magick_number_threads(denominator_image,divide_image,divide_image->rows,1)
2814 for (y=0; y < (ssize_t) divide_image->rows; y++)
2825 if (status == MagickFalse)
2827 p=GetCacheViewVirtualPixels(denominator_view,0,y,
2828 denominator_image->columns,1,exception);
2829 q=GetCacheViewAuthenticPixels(numerator_view,0,y,divide_image->columns,1,
2831 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2836 for (x=0; x < (ssize_t) divide_image->columns; x++)
2841 for (i=0; i < (ssize_t) GetPixelChannels(divide_image); i++)
2843 PixelChannel channel = GetPixelChannelChannel(divide_image,i);
2844 PixelTrait traits = GetPixelChannelTraits(divide_image,channel);
2845 PixelTrait denominator_traits = GetPixelChannelTraits(denominator_image,
2847 if (((traits & UpdatePixelTrait) == 0) ||
2848 ((denominator_traits & UpdatePixelTrait) == 0))
2850 q[i]=(Quantum) ((
double) q[i]*MagickSafeReciprocal(QuantumScale*
2851 (
double) GetPixelChannel(denominator_image,channel,p)));
2853 p+=(ptrdiff_t) GetPixelChannels(denominator_image);
2854 q+=(ptrdiff_t) GetPixelChannels(divide_image);
2856 if (SyncCacheViewAuthenticPixels(numerator_view,exception) == MagickFalse)
2859 denominator_view=DestroyCacheView(denominator_view);
2860 numerator_view=DestroyCacheView(numerator_view);
2861 if (status == MagickFalse)
2862 divide_image=DestroyImage(divide_image);
2863 return(divide_image);
2866static Image *SIMDivideByMagnitude(Image *image,Image *magnitude_image,
2867 const Image *source_image,ExceptionInfo *exception)
2876 divide_image=SIMDivideImage(image,magnitude_image,exception);
2877 if (divide_image == (Image *) NULL)
2878 return((Image *) NULL);
2879 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
2880 ÷_image->background_color);
2881 SetGeometry(source_image,&geometry);
2882 geometry.width=MagickMax(source_image->columns,divide_image->columns);
2883 geometry.height=MagickMax(source_image->rows,divide_image->rows);
2884 result_image=ExtentImage(divide_image,&geometry,exception);
2885 divide_image=DestroyImage(divide_image);
2886 return(result_image);
2889static MagickBooleanType SIMFilterImageNaNs(Image *image,
2890 ExceptionInfo *exception)
2896 status = MagickTrue;
2904 image_view=AcquireAuthenticCacheView(image,exception);
2905#if defined(MAGICKCORE_OPENMP_SUPPORT)
2906 #pragma omp parallel for schedule(static) shared(status) \
2907 magick_number_threads(image,image,image->rows,1)
2909 for (y=0; y < (ssize_t) image->rows; y++)
2917 if (status == MagickFalse)
2919 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2920 if (q == (Quantum *) NULL)
2925 for (x=0; x < (ssize_t) image->columns; x++)
2930 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
2932 PixelChannel channel = GetPixelChannelChannel(image,i);
2933 PixelTrait traits = GetPixelChannelTraits(image,channel);
2934 if ((traits & UpdatePixelTrait) == 0)
2936 if (IsNaN((
double) q[i]) != 0)
2939 q+=(ptrdiff_t) GetPixelChannels(image);
2941 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2944 image_view=DestroyCacheView(image_view);
2948static Image *SIMSquareImage(
const Image *image,ExceptionInfo *exception)
2957 status = MagickTrue;
2965 square_image=CloneImage(image,0,0,MagickTrue,exception);
2966 if (square_image == (Image *) NULL)
2967 return(square_image);
2968 image_view=AcquireAuthenticCacheView(square_image,exception);
2969#if defined(MAGICKCORE_OPENMP_SUPPORT)
2970 #pragma omp parallel for schedule(static) shared(status) \
2971 magick_number_threads(square_image,square_image,square_image->rows,1)
2973 for (y=0; y < (ssize_t) square_image->rows; y++)
2981 if (status == MagickFalse)
2983 q=GetCacheViewAuthenticPixels(image_view,0,y,square_image->columns,1,
2985 if (q == (Quantum *) NULL)
2990 for (x=0; x < (ssize_t) square_image->columns; x++)
2995 for (i=0; i < (ssize_t) GetPixelChannels(square_image); i++)
2997 PixelChannel channel = GetPixelChannelChannel(square_image,i);
2998 PixelTrait traits = GetPixelChannelTraits(square_image,channel);
2999 if ((traits & UpdatePixelTrait) == 0)
3001 q[i]=(Quantum) (QuantumScale*q[i]*q[i]);
3003 q+=(ptrdiff_t) GetPixelChannels(square_image);
3005 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3008 image_view=DestroyCacheView(image_view);
3009 if (status == MagickFalse)
3010 square_image=DestroyImage(square_image);
3011 return(square_image);
3014static Image *SIMMagnitudeImage(Image *alpha_image,Image *beta_image,
3015 ExceptionInfo *exception)
3023 status = MagickTrue;
3025 (void) SetImageArtifact(alpha_image,
"compose:clamp",
"False");
3026 xsq_image=SIMSquareImage(alpha_image,exception);
3027 if (xsq_image == (Image *) NULL)
3028 return((Image *) NULL);
3029 (void) SetImageArtifact(beta_image,
"compose:clamp",
"False");
3030 ysq_image=SIMSquareImage(beta_image,exception);
3031 if (ysq_image == (Image *) NULL)
3033 xsq_image=DestroyImage(xsq_image);
3034 return((Image *) NULL);
3036 status=CompositeImage(xsq_image,ysq_image,PlusCompositeOp,MagickTrue,0,0,
3038 magnitude_image=xsq_image;
3039 ysq_image=DestroyImage(ysq_image);
3040 if (status == MagickFalse)
3042 magnitude_image=DestroyImage(magnitude_image);
3043 return((Image *) NULL);
3045 status=EvaluateImage(magnitude_image,PowEvaluateOperator,0.5,exception);
3046 if (status == MagickFalse)
3048 magnitude_image=DestroyImage(magnitude_image);
3049 return (Image *) NULL;
3051 return(magnitude_image);
3054static MagickBooleanType SIMMaximaImage(
const Image *image,
double *maxima,
3055 RectangleInfo *offset,ExceptionInfo *exception)
3074 status = MagickTrue;
3077 maxima_info = { -MagickMaximumValue, 0, 0 };
3085 image_view=AcquireVirtualCacheView(image,exception);
3086 q=GetCacheViewVirtualPixels(image_view,maxima_info.x,maxima_info.y,1,1,
3088 if (q != (
const Quantum *) NULL)
3089 maxima_info.maxima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3090#if defined(MAGICKCORE_OPENMP_SUPPORT)
3091 #pragma omp parallel for schedule(static) shared(maxima_info,status) \
3092 magick_number_threads(image,image,image->rows,1)
3094 for (y=0; y < (ssize_t) image->rows; y++)
3100 channel_maxima = { -MagickMaximumValue, 0, 0 };
3105 if (status == MagickFalse)
3107 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3108 if (p == (
const Quantum *) NULL)
3113 channel_maxima=maxima_info;
3114 for (x=0; x < (ssize_t) image->columns; x++)
3119 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3124 PixelChannel channel = GetPixelChannelChannel(image,i);
3125 PixelTrait traits = GetPixelChannelTraits(image,channel);
3126 if ((traits & UpdatePixelTrait) == 0)
3128 pixel=(double) p[i];
3129 if (IsNaN(pixel) != 0)
3131 if (pixel > channel_maxima.maxima)
3133 channel_maxima.maxima=(double) p[i];
3138 p+=(ptrdiff_t) GetPixelChannels(image);
3140#if defined(MAGICKCORE_OPENMP_SUPPORT)
3141 #pragma omp critical (MagickCore_SIMMaximaImage)
3143 if (channel_maxima.maxima > maxima_info.maxima)
3144 maxima_info=channel_maxima;
3146 image_view=DestroyCacheView(image_view);
3147 *maxima=maxima_info.maxima;
3148 offset->x=maxima_info.x;
3149 offset->y=maxima_info.y;
3153static MagickBooleanType SIMMinimaImage(
const Image *image,
double *minima,
3154 RectangleInfo *offset,ExceptionInfo *exception)
3173 status = MagickTrue;
3176 minima_info = { MagickMaximumValue, 0, 0 };
3184 image_view=AcquireVirtualCacheView(image,exception);
3185 q=GetCacheViewVirtualPixels(image_view,minima_info.x,minima_info.y,1,1,
3187 if (q != (
const Quantum *) NULL)
3188 minima_info.minima=IsNaN((
double) q[0]) != 0 ? 0.0 : (double) q[0];
3189#if defined(MAGICKCORE_OPENMP_SUPPORT)
3190 #pragma omp parallel for schedule(static) shared(minima_info,status) \
3191 magick_number_threads(image,image,image->rows,1)
3193 for (y=0; y < (ssize_t) image->rows; y++)
3199 channel_minima = { MagickMaximumValue, 0, 0 };
3204 if (status == MagickFalse)
3206 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
3207 if (p == (
const Quantum *) NULL)
3212 channel_minima=minima_info;
3213 for (x=0; x < (ssize_t) image->columns; x++)
3218 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3223 PixelChannel channel = GetPixelChannelChannel(image,i);
3224 PixelTrait traits = GetPixelChannelTraits(image,channel);
3225 if ((traits & UpdatePixelTrait) == 0)
3227 pixel=(double) p[i];
3228 if (IsNaN(pixel) != 0)
3230 if (pixel < channel_minima.minima)
3232 channel_minima.minima=pixel;
3237 p+=(ptrdiff_t) GetPixelChannels(image);
3239#if defined(MAGICKCORE_OPENMP_SUPPORT)
3240 #pragma omp critical (MagickCore_SIMMinimaImage)
3242 if (channel_minima.minima < minima_info.minima)
3243 minima_info=channel_minima;
3245 image_view=DestroyCacheView(image_view);
3246 *minima=minima_info.minima;
3247 offset->x=minima_info.x;
3248 offset->y=minima_info.y;
3252static MagickBooleanType SIMMultiplyImage(Image *image,
const double factor,
3253 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3259 status = MagickTrue;
3267 image_view=AcquireAuthenticCacheView(image,exception);
3268#if defined(MAGICKCORE_OPENMP_SUPPORT)
3269 #pragma omp parallel for schedule(static) shared(status) \
3270 magick_number_threads(image,image,image->rows,1)
3272 for (y=0; y < (ssize_t) image->rows; y++)
3280 if (status == MagickFalse)
3282 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3283 if (q == (Quantum *) NULL)
3288 for (x=0; x < (ssize_t) image->columns; x++)
3293 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3295 PixelChannel channel = GetPixelChannelChannel(image,i);
3296 PixelTrait traits = GetPixelChannelTraits(image,channel);
3297 if ((traits & UpdatePixelTrait) == 0)
3299 if (channel_statistics != (
const ChannelStatistics *) NULL)
3300 q[i]=(Quantum) (factor*q[i]*QuantumScale*
3301 channel_statistics[channel].standard_deviation);
3303 q[i]=(Quantum) (factor*q[i]);
3305 q+=(ptrdiff_t) GetPixelChannels(image);
3307 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3310 image_view=DestroyCacheView(image_view);
3314static Image *SIMPhaseCorrelationImage(
const Image *alpha_image,
3315 const Image *beta_image,
const Image *magnitude_image,ExceptionInfo *exception)
3318 *alpha_fft = (Image *) NULL,
3319 *beta_fft = (Image *) NULL,
3320 *complex_multiplication = (Image *) NULL,
3321 *cross_correlation = (Image *) NULL;
3326 beta_fft=CloneImage(beta_image,0,0,MagickTrue,exception);
3327 if (beta_fft == NULL)
3328 return((Image *) NULL);
3329 (void) SetImageArtifact(beta_fft,
"fourier:normalize",
"inverse");
3330 beta_fft=ForwardFourierTransformImage(beta_fft,MagickFalse,exception);
3331 if (beta_fft == NULL)
3332 return((Image *) NULL);
3336 alpha_fft=CloneImage(alpha_image,0,0,MagickTrue,exception);
3337 if (alpha_fft == (Image *) NULL)
3339 beta_fft=DestroyImageList(beta_fft);
3340 return((Image *) NULL);
3342 (void) SetImageArtifact(alpha_fft,
"fourier:normalize",
"inverse");
3343 alpha_fft=ForwardFourierTransformImage(alpha_fft,MagickFalse,exception);
3344 if (alpha_fft == (Image *) NULL)
3346 beta_fft=DestroyImageList(beta_fft);
3347 return((Image *) NULL);
3352 beta_fft=ComplexImages(beta_fft,ConjugateComplexOperator,exception);
3353 if (beta_fft == (Image *) NULL)
3355 alpha_fft=DestroyImageList(alpha_fft);
3356 return((Image *) NULL);
3361 AppendImageToList(&beta_fft,alpha_fft);
3362 DisableCompositeClampUnlessSpecified(beta_fft);
3363 DisableCompositeClampUnlessSpecified(beta_fft->next);
3364 complex_multiplication=ComplexImages(beta_fft,MultiplyComplexOperator,
3366 beta_fft=DestroyImageList(beta_fft);
3367 if (complex_multiplication == (Image *) NULL)
3368 return((Image *) NULL);
3372 CompositeLayers(complex_multiplication,DivideSrcCompositeOp,(Image *)
3373 magnitude_image,0,0,exception);
3377 (void) SetImageArtifact(complex_multiplication,
"fourier:normalize",
"inverse");
3378 cross_correlation=InverseFourierTransformImage(complex_multiplication,
3379 complex_multiplication->next,MagickFalse,exception);
3380 complex_multiplication=DestroyImageList(complex_multiplication);
3381 return(cross_correlation);
3384static MagickBooleanType SIMSetImageMean(Image *image,
3385 const ChannelStatistics *channel_statistics,ExceptionInfo *exception)
3391 status = MagickTrue;
3399 image_view=AcquireAuthenticCacheView(image,exception);
3400#if defined(MAGICKCORE_OPENMP_SUPPORT)
3401 #pragma omp parallel for schedule(static) shared(status) \
3402 magick_number_threads(image,image,image->rows,1)
3404 for (y=0; y < (ssize_t) image->rows; y++)
3412 if (status == MagickFalse)
3414 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
3415 if (q == (Quantum *) NULL)
3420 for (x=0; x < (ssize_t) image->columns; x++)
3425 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
3427 PixelChannel channel = GetPixelChannelChannel(image,i);
3428 PixelTrait traits = GetPixelChannelTraits(image,channel);
3429 if ((traits & UpdatePixelTrait) == 0)
3431 q[i]=(Quantum) channel_statistics[channel].mean;
3433 q+=(ptrdiff_t) GetPixelChannels(image);
3435 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3438 image_view=DestroyCacheView(image_view);
3442static Image *SIMSubtractImageMean(
const Image *alpha_image,
3443 const Image *beta_image,
const ChannelStatistics *channel_statistics,
3444 ExceptionInfo *exception)
3454 status = MagickTrue;
3462 subtract_image=CloneImage(beta_image,alpha_image->columns,alpha_image->rows,
3463 MagickTrue,exception);
3464 if (subtract_image == (Image *) NULL)
3465 return(subtract_image);
3466 image_view=AcquireAuthenticCacheView(subtract_image,exception);
3467 beta_view=AcquireVirtualCacheView(beta_image,exception);
3468#if defined(MAGICKCORE_OPENMP_SUPPORT)
3469 #pragma omp parallel for schedule(static) shared(status) \
3470 magick_number_threads(beta_image,subtract_image,subtract_image->rows,1)
3472 for (y=0; y < (ssize_t) subtract_image->rows; y++)
3483 if (status == MagickFalse)
3485 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,exception);
3486 q=GetCacheViewAuthenticPixels(image_view,0,y,subtract_image->columns,1,
3488 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3493 for (x=0; x < (ssize_t) subtract_image->columns; x++)
3498 for (i=0; i < (ssize_t) GetPixelChannels(subtract_image); i++)
3500 PixelChannel channel = GetPixelChannelChannel(subtract_image,i);
3501 PixelTrait traits = GetPixelChannelTraits(subtract_image,channel);
3502 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3503 if (((traits & UpdatePixelTrait) == 0) ||
3504 ((beta_traits & UpdatePixelTrait) == 0))
3506 if ((x >= (ssize_t) beta_image->columns) ||
3507 (y >= (ssize_t) beta_image->rows))
3510 q[i]=(Quantum) ((
double) GetPixelChannel(beta_image,channel,p)-
3511 channel_statistics[channel].mean);
3513 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3514 q+=(ptrdiff_t) GetPixelChannels(subtract_image);
3516 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3519 beta_view=DestroyCacheView(beta_view);
3520 image_view=DestroyCacheView(image_view);
3521 if (status == MagickFalse)
3522 subtract_image=DestroyImage(subtract_image);
3523 return(subtract_image);
3526static Image *SIMUnityImage(
const Image *alpha_image,
const Image *beta_image,
3527 ExceptionInfo *exception)
3536 status = MagickTrue;
3544 unity_image=CloneImage(alpha_image,alpha_image->columns,alpha_image->rows,
3545 MagickTrue,exception);
3546 if (unity_image == (Image *) NULL)
3547 return(unity_image);
3548 if (SetImageStorageClass(unity_image,DirectClass,exception) == MagickFalse)
3549 return(DestroyImage(unity_image));
3550 image_view=AcquireAuthenticCacheView(unity_image,exception);
3551#if defined(MAGICKCORE_OPENMP_SUPPORT)
3552 #pragma omp parallel for schedule(static) shared(status) \
3553 magick_number_threads(unity_image,unity_image,unity_image->rows,1)
3555 for (y=0; y < (ssize_t) unity_image->rows; y++)
3563 if (status == MagickFalse)
3565 q=GetCacheViewAuthenticPixels(image_view,0,y,unity_image->columns,1,
3567 if (q == (Quantum *) NULL)
3572 for (x=0; x < (ssize_t) unity_image->columns; x++)
3577 for (i=0; i < (ssize_t) GetPixelChannels(unity_image); i++)
3579 PixelChannel channel = GetPixelChannelChannel(unity_image,i);
3580 PixelTrait traits = GetPixelChannelTraits(unity_image,channel);
3581 if ((traits & UpdatePixelTrait) == 0)
3583 if ((x >= (ssize_t) beta_image->columns) ||
3584 (y >= (ssize_t) beta_image->rows))
3589 q+=(ptrdiff_t) GetPixelChannels(unity_image);
3591 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3594 image_view=DestroyCacheView(image_view);
3595 if (status == MagickFalse)
3596 unity_image=DestroyImage(unity_image);
3597 return(unity_image);
3600static Image *SIMVarianceImage(Image *alpha_image,
const Image *beta_image,
3601 ExceptionInfo *exception)
3611 status = MagickTrue;
3619 variance_image=CloneImage(alpha_image,0,0,MagickTrue,exception);
3620 if (variance_image == (Image *) NULL)
3621 return(variance_image);
3622 image_view=AcquireAuthenticCacheView(variance_image,exception);
3623 beta_view=AcquireVirtualCacheView(beta_image,exception);
3624#if defined(MAGICKCORE_OPENMP_SUPPORT)
3625 #pragma omp parallel for schedule(static) shared(status) \
3626 magick_number_threads(beta_image,variance_image,variance_image->rows,1)
3628 for (y=0; y < (ssize_t) variance_image->rows; y++)
3639 if (status == MagickFalse)
3641 p=GetCacheViewVirtualPixels(beta_view,0,y,beta_image->columns,1,
3643 q=GetCacheViewAuthenticPixels(image_view,0,y,variance_image->columns,1,
3645 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
3650 for (x=0; x < (ssize_t) variance_image->columns; x++)
3655 for (i=0; i < (ssize_t) GetPixelChannels(variance_image); i++)
3660 PixelChannel channel = GetPixelChannelChannel(variance_image,i);
3661 PixelTrait traits = GetPixelChannelTraits(variance_image,channel);
3662 PixelTrait beta_traits = GetPixelChannelTraits(beta_image,channel);
3663 if (((traits & UpdatePixelTrait) == 0) ||
3664 ((beta_traits & UpdatePixelTrait) == 0))
3666 error=(double) q[i]-(
double) GetPixelChannel(beta_image,channel,p);
3667 q[i]=(Quantum) ((
double) ClampToQuantum((
double) QuantumRange*
3668 (sqrt(fabs(QuantumScale*error))/sqrt((
double) QuantumRange))));
3670 p+=(ptrdiff_t) GetPixelChannels(beta_image);
3671 q+=(ptrdiff_t) GetPixelChannels(variance_image);
3673 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3676 beta_view=DestroyCacheView(beta_view);
3677 image_view=DestroyCacheView(image_view);
3678 if (status == MagickFalse)
3679 variance_image=DestroyImage(variance_image);
3680 return(variance_image);
3683static Image *DPCSimilarityImage(
const Image *image,
const Image *reconstruct,
3684 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
3686#define ThrowDPCSimilarityException() \
3688 if (dot_product_image != (Image *) NULL) \
3689 dot_product_image=DestroyImage(dot_product_image); \
3690 if (magnitude_image != (Image *) NULL) \
3691 magnitude_image=DestroyImage(magnitude_image); \
3692 if (reconstruct_image != (Image *) NULL) \
3693 reconstruct_image=DestroyImage(reconstruct_image); \
3694 if (rx_image != (Image *) NULL) \
3695 rx_image=DestroyImage(rx_image); \
3696 if (ry_image != (Image *) NULL) \
3697 ry_image=DestroyImage(ry_image); \
3698 if (target_image != (Image *) NULL) \
3699 target_image=DestroyImage(target_image); \
3700 if (threshold_image != (Image *) NULL) \
3701 threshold_image=DestroyImage(threshold_image); \
3702 if (trx_image != (Image *) NULL) \
3703 trx_image=DestroyImage(trx_image); \
3704 if (try_image != (Image *) NULL) \
3705 try_image=DestroyImage(try_image); \
3706 if (tx_image != (Image *) NULL) \
3707 tx_image=DestroyImage(tx_image); \
3708 if (ty_image != (Image *) NULL) \
3709 ty_image=DestroyImage(ty_image); \
3710 return((Image *) NULL); \
3717 standard_deviation = 0.0;
3720 *dot_product_image = (Image *) NULL,
3721 *magnitude_image = (Image *) NULL,
3722 *reconstruct_image = (Image *) NULL,
3723 *rx_image = (Image *) NULL,
3724 *ry_image = (Image *) NULL,
3725 *trx_image = (Image *) NULL,
3726 *target_image = (Image *) NULL,
3727 *threshold_image = (Image *) NULL,
3728 *try_image = (Image *) NULL,
3729 *tx_image = (Image *) NULL,
3730 *ty_image = (Image *) NULL;
3733 status = MagickTrue;
3741 target_image=CloneImage(image,0,0,MagickTrue,exception);
3742 if (target_image == (Image *) NULL)
3743 return((Image *) NULL);
3747 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
3748 if (reconstruct_image == (Image *) NULL)
3749 ThrowDPCSimilarityException();
3753 (void) SetImageVirtualPixelMethod(reconstruct_image,EdgeVirtualPixelMethod,
3755 rx_image=SIMDerivativeImage(reconstruct_image,
"Sobel",exception);
3756 if (rx_image == (Image *) NULL)
3757 ThrowDPCSimilarityException();
3758 ry_image=SIMDerivativeImage(reconstruct_image,
"Sobel:90",exception);
3759 reconstruct_image=DestroyImage(reconstruct_image);
3760 if (ry_image == (Image *) NULL)
3761 ThrowDPCSimilarityException();
3765 magnitude_image=SIMMagnitudeImage(rx_image,ry_image,exception);
3766 if (magnitude_image == (Image *) NULL)
3767 ThrowDPCSimilarityException();
3771 threshold_image=CloneImage(magnitude_image,0,0,MagickTrue,exception);
3772 if (threshold_image == (Image *) NULL)
3773 ThrowDPCSimilarityException();
3774 status=BilevelImage(threshold_image,0.0,exception);
3775 if (status == MagickFalse)
3776 ThrowDPCSimilarityException();
3777 status=GetImageMean(threshold_image,&mean,&standard_deviation,exception);
3778 threshold_image=DestroyImage(threshold_image);
3779 if (status == MagickFalse)
3780 ThrowDPCSimilarityException();
3781 edge_factor=MagickSafeReciprocal(QuantumScale*mean*reconstruct->columns*
3782 reconstruct->rows)+QuantumScale;
3786 trx_image=SIMDivideByMagnitude(rx_image,magnitude_image,image,exception);
3787 rx_image=DestroyImage(rx_image);
3788 if (trx_image == (Image *) NULL)
3789 ThrowDPCSimilarityException();
3791 try_image=SIMDivideByMagnitude(ry_image,magnitude_image,image,exception);
3792 magnitude_image=DestroyImage(magnitude_image);
3793 ry_image=DestroyImage(ry_image);
3794 if (try_image == (Image *) NULL)
3795 ThrowDPCSimilarityException();
3800 (void) SetImageVirtualPixelMethod(target_image,EdgeVirtualPixelMethod,
3802 tx_image=SIMDerivativeImage(target_image,
"Sobel",exception);
3803 if (tx_image == (Image *) NULL)
3804 ThrowDPCSimilarityException();
3805 ty_image=SIMDerivativeImage(target_image,
"Sobel:90",exception);
3806 target_image=DestroyImage(target_image);
3807 if (ty_image == (Image *) NULL)
3808 ThrowDPCSimilarityException();
3812 magnitude_image=SIMMagnitudeImage(tx_image,ty_image,exception);
3813 if (magnitude_image == (Image *) NULL)
3814 ThrowDPCSimilarityException();
3818 trx_image=SIMDivideByMagnitude(tx_image,magnitude_image,image,exception);
3819 tx_image=DestroyImage(tx_image);
3820 if (trx_image == (Image *) NULL)
3821 ThrowDPCSimilarityException();
3823 try_image=SIMDivideByMagnitude(ty_image,magnitude_image,image,exception);
3824 ty_image=DestroyImage(ty_image);
3825 magnitude_image=DestroyImage(magnitude_image);
3826 if (try_image == (Image *) NULL)
3827 ThrowDPCSimilarityException();
3832 trx_image=SIMCrossCorrelationImage(tx_image,rx_image,exception);
3833 rx_image=DestroyImage(rx_image);
3834 tx_image=DestroyImage(tx_image);
3835 if (trx_image == (Image *) NULL)
3836 ThrowDPCSimilarityException();
3837 try_image=SIMCrossCorrelationImage(ty_image,ry_image,exception);
3838 ry_image=DestroyImage(ry_image);
3839 ty_image=DestroyImage(ty_image);
3840 if (try_image == (Image *) NULL)
3841 ThrowDPCSimilarityException();
3845 (void) SetImageArtifact(try_image,
"compose:clamp",
"false");
3846 status=CompositeImage(trx_image,try_image,PlusCompositeOp,MagickTrue,0,0,
3848 try_image=DestroyImage(try_image);
3849 if (status == MagickFalse)
3850 ThrowDPCSimilarityException();
3851 status=SIMMultiplyImage(trx_image,edge_factor,
3852 (
const ChannelStatistics *) NULL,exception);
3853 if (status == MagickFalse)
3854 ThrowDPCSimilarityException();
3858 SetGeometry(image,&geometry);
3859 geometry.width=image->columns;
3860 geometry.height=image->rows;
3861 (void) ResetImagePage(trx_image,
"0x0+0+0");
3862 dot_product_image=CropImage(trx_image,&geometry,exception);
3863 trx_image=DestroyImage(trx_image);
3864 if (dot_product_image == (Image *) NULL)
3865 ThrowDPCSimilarityException();
3866 (void) ResetImagePage(dot_product_image,
"0x0+0+0");
3870 status=GrayscaleImage(dot_product_image,AveragePixelIntensityMethod,
3872 if (status == MagickFalse)
3873 ThrowDPCSimilarityException();
3874 dot_product_image->depth=32;
3875 dot_product_image->colorspace=GRAYColorspace;
3876 dot_product_image->alpha_trait=UndefinedPixelTrait;
3877 status=SIMFilterImageNaNs(dot_product_image,exception);
3878 if (status == MagickFalse)
3879 ThrowDPCSimilarityException();
3880 status=SIMMaximaImage(dot_product_image,&maxima,offset,exception);
3881 if (status == MagickFalse)
3882 ThrowDPCSimilarityException();
3883 if ((QuantumScale*maxima) > 1.0)
3885 status=SIMMultiplyImage(dot_product_image,1.0/(QuantumScale*maxima),
3886 (
const ChannelStatistics *) NULL,exception);
3887 maxima=(double) QuantumRange;
3889 *similarity_metric=QuantumScale*maxima;
3890 return(dot_product_image);
3893static Image *MSESimilarityImage(
const Image *image,
const Image *reconstruct,
3894 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
3896#define ThrowMSESimilarityException() \
3898 if (alpha_image != (Image *) NULL) \
3899 alpha_image=DestroyImage(alpha_image); \
3900 if (beta_image != (Image *) NULL) \
3901 beta_image=DestroyImage(beta_image); \
3902 if (channel_statistics != (ChannelStatistics *) NULL) \
3903 channel_statistics=(ChannelStatistics *) \
3904 RelinquishMagickMemory(channel_statistics); \
3905 if (mean_image != (Image *) NULL) \
3906 mean_image=DestroyImage(mean_image); \
3907 if (mse_image != (Image *) NULL) \
3908 mse_image=DestroyImage(mse_image); \
3909 if (reconstruct_image != (Image *) NULL) \
3910 reconstruct_image=DestroyImage(reconstruct_image); \
3911 if (sum_image != (Image *) NULL) \
3912 sum_image=DestroyImage(sum_image); \
3913 if (alpha_image != (Image *) NULL) \
3914 alpha_image=DestroyImage(alpha_image); \
3915 return((Image *) NULL); \
3919 *channel_statistics = (ChannelStatistics *) NULL;
3925 *alpha_image = (Image *) NULL,
3926 *beta_image = (Image *) NULL,
3927 *mean_image = (Image *) NULL,
3928 *mse_image = (Image *) NULL,
3929 *reconstruct_image = (Image *) NULL,
3930 *sum_image = (Image *) NULL,
3931 *target_image = (Image *) NULL;
3934 status = MagickTrue;
3942 target_image=SIMSquareImage(image,exception);
3943 if (target_image == (Image *) NULL)
3944 ThrowMSESimilarityException();
3945 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
3946 if (reconstruct_image == (Image *) NULL)
3947 ThrowMSESimilarityException();
3951 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
3953 target_image=DestroyImage(target_image);
3954 if (alpha_image == (Image *) NULL)
3955 ThrowMSESimilarityException();
3956 status=SIMMultiplyImage(alpha_image,1.0/reconstruct->columns/(
double)
3957 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
3958 if (status == MagickFalse)
3959 ThrowMSESimilarityException();
3963 (void) CompositeImage(reconstruct_image,reconstruct,CopyCompositeOp,
3964 MagickTrue,0,0,exception);
3965 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
3966 reconstruct_image=DestroyImage(reconstruct_image);
3967 if (beta_image == (Image *) NULL)
3968 ThrowMSESimilarityException();
3969 status=SIMMultiplyImage(beta_image,-2.0/reconstruct->columns/(
double)
3970 reconstruct->rows,(
const ChannelStatistics *) NULL,exception);
3971 if (status == MagickFalse)
3972 ThrowMSESimilarityException();
3976 sum_image=SIMSquareImage(reconstruct,exception);
3977 if (sum_image == (Image *) NULL)
3978 ThrowMSESimilarityException();
3979 channel_statistics=GetImageStatistics(sum_image,exception);
3980 if (channel_statistics == (ChannelStatistics *) NULL)
3981 ThrowMSESimilarityException();
3982 status=SetImageStorageClass(sum_image,DirectClass,exception);
3983 if (status == MagickFalse)
3984 ThrowMSESimilarityException();
3985 status=SIMSetImageMean(sum_image,channel_statistics,exception);
3986 channel_statistics=(ChannelStatistics *)
3987 RelinquishMagickMemory(channel_statistics);
3988 if (status == MagickFalse)
3989 ThrowMSESimilarityException();
3993 AppendImageToList(&sum_image,alpha_image);
3994 AppendImageToList(&sum_image,beta_image);
3995 mean_image=EvaluateImages(sum_image,SumEvaluateOperator,exception);
3996 if (mean_image == (Image *) NULL)
3997 ThrowMSESimilarityException();
3998 sum_image=DestroyImage(sum_image);
3999 status=GrayscaleImage(mean_image,AveragePixelIntensityMethod,exception);
4000 if (status == MagickFalse)
4001 ThrowMSESimilarityException();
4005 SetGeometry(image,&geometry);
4006 geometry.width=image->columns;
4007 geometry.height=image->rows;
4008 (void) ResetImagePage(mean_image,
"0x0+0+0");
4009 mse_image=CropImage(mean_image,&geometry,exception);
4010 mean_image=DestroyImage(mean_image);
4011 if (mse_image == (Image *) NULL)
4012 ThrowMSESimilarityException();
4016 (void) ResetImagePage(mse_image,
"0x0+0+0");
4017 (void) ClampImage(mse_image,exception);
4018 mse_image->depth=32;
4019 mse_image->colorspace=GRAYColorspace;
4020 mse_image->alpha_trait=UndefinedPixelTrait;
4021 status=SIMMinimaImage(mse_image,&minima,offset,exception);
4022 if (status == MagickFalse)
4023 ThrowMSESimilarityException();
4024 status=NegateImage(mse_image,MagickFalse,exception);
4025 if (status == MagickFalse)
4026 ThrowMSESimilarityException();
4027 alpha_image=DestroyImage(alpha_image);
4028 beta_image=DestroyImage(beta_image);
4029 if ((QuantumScale*minima) < FLT_EPSILON)
4031 *similarity_metric=QuantumScale*minima;
4035static Image *NCCSimilarityImage(
const Image *image,
const Image *reconstruct,
4036 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4038#define ThrowNCCSimilarityException() \
4040 if (alpha_image != (Image *) NULL) \
4041 alpha_image=DestroyImage(alpha_image); \
4042 if (beta_image != (Image *) NULL) \
4043 beta_image=DestroyImage(beta_image); \
4044 if (channel_statistics != (ChannelStatistics *) NULL) \
4045 channel_statistics=(ChannelStatistics *) \
4046 RelinquishMagickMemory(channel_statistics); \
4047 if (correlation_image != (Image *) NULL) \
4048 correlation_image=DestroyImage(correlation_image); \
4049 if (divide_image != (Image *) NULL) \
4050 divide_image=DestroyImage(divide_image); \
4051 if (ncc_image != (Image *) NULL) \
4052 ncc_image=DestroyImage(ncc_image); \
4053 if (normalize_image != (Image *) NULL) \
4054 normalize_image=DestroyImage(normalize_image); \
4055 if (reconstruct_image != (Image *) NULL) \
4056 reconstruct_image=DestroyImage(reconstruct_image); \
4057 if (target_image != (Image *) NULL) \
4058 target_image=DestroyImage(target_image); \
4059 if (variance_image != (Image *) NULL) \
4060 variance_image=DestroyImage(variance_image); \
4061 return((Image *) NULL); \
4065 *channel_statistics = (ChannelStatistics *) NULL;
4071 *alpha_image = (Image *) NULL,
4072 *beta_image = (Image *) NULL,
4073 *correlation_image = (Image *) NULL,
4074 *divide_image = (Image *) NULL,
4075 *ncc_image = (Image *) NULL,
4076 *normalize_image = (Image *) NULL,
4077 *reconstruct_image = (Image *) NULL,
4078 *target_image = (Image *) NULL,
4079 *variance_image = (Image *) NULL;
4082 status = MagickTrue;
4090 target_image=SIMSquareImage(image,exception);
4091 if (target_image == (Image *) NULL)
4092 ThrowNCCSimilarityException();
4093 reconstruct_image=SIMUnityImage(image,reconstruct,exception);
4094 if (reconstruct_image == (Image *) NULL)
4095 ThrowNCCSimilarityException();
4099 alpha_image=SIMCrossCorrelationImage(target_image,reconstruct_image,
4101 target_image=DestroyImage(target_image);
4102 if (alpha_image == (Image *) NULL)
4103 ThrowNCCSimilarityException();
4104 status=SIMMultiplyImage(alpha_image,(
double) QuantumRange*
4105 reconstruct->columns*reconstruct->rows,(
const ChannelStatistics *) NULL,
4107 if (status == MagickFalse)
4108 ThrowNCCSimilarityException();
4112 beta_image=SIMCrossCorrelationImage(image,reconstruct_image,exception);
4113 reconstruct_image=DestroyImage(reconstruct_image);
4114 if (beta_image == (Image *) NULL)
4115 ThrowNCCSimilarityException();
4116 target_image=SIMSquareImage(beta_image,exception);
4117 beta_image=DestroyImage(beta_image);
4118 if (target_image == (Image *) NULL)
4119 ThrowNCCSimilarityException();
4120 status=SIMMultiplyImage(target_image,(
double) QuantumRange,
4121 (
const ChannelStatistics *) NULL,exception);
4122 if (status == MagickFalse)
4123 ThrowNCCSimilarityException();
4127 variance_image=SIMVarianceImage(alpha_image,target_image,exception);
4128 target_image=DestroyImage(target_image);
4129 alpha_image=DestroyImage(alpha_image);
4130 if (variance_image == (Image *) NULL)
4131 ThrowNCCSimilarityException();
4135 channel_statistics=GetImageStatistics(reconstruct,exception);
4136 if (channel_statistics == (ChannelStatistics *) NULL)
4137 ThrowNCCSimilarityException();
4138 status=SIMMultiplyImage(variance_image,1.0,channel_statistics,exception);
4139 if (status == MagickFalse)
4140 ThrowNCCSimilarityException();
4141 normalize_image=SIMSubtractImageMean(image,reconstruct,channel_statistics,
4143 channel_statistics=(ChannelStatistics *)
4144 RelinquishMagickMemory(channel_statistics);
4145 if (normalize_image == (Image *) NULL)
4146 ThrowNCCSimilarityException();
4147 correlation_image=SIMCrossCorrelationImage(image,normalize_image,exception);
4148 normalize_image=DestroyImage(normalize_image);
4149 if (correlation_image == (Image *) NULL)
4150 ThrowNCCSimilarityException();
4154 divide_image=SIMDivideImage(correlation_image,variance_image,exception);
4155 correlation_image=DestroyImage(correlation_image);
4156 variance_image=DestroyImage(variance_image);
4157 if (divide_image == (Image *) NULL)
4158 ThrowNCCSimilarityException();
4162 SetGeometry(image,&geometry);
4163 geometry.width=image->columns;
4164 geometry.height=image->rows;
4165 (void) ResetImagePage(divide_image,
"0x0+0+0");
4166 ncc_image=CropImage(divide_image,&geometry,exception);
4167 divide_image=DestroyImage(divide_image);
4168 if (ncc_image == (Image *) NULL)
4169 ThrowNCCSimilarityException();
4173 (void) ResetImagePage(ncc_image,
"0x0+0+0");
4174 status=GrayscaleImage(ncc_image,AveragePixelIntensityMethod,exception);
4175 if (status == MagickFalse)
4176 ThrowNCCSimilarityException();
4177 ncc_image->depth=32;
4178 ncc_image->colorspace=GRAYColorspace;
4179 ncc_image->alpha_trait=UndefinedPixelTrait;
4180 status=SIMMaximaImage(ncc_image,&maxima,offset,exception);
4181 if (status == MagickFalse)
4182 ThrowNCCSimilarityException();
4183 if ((QuantumScale*maxima) > 1.0)
4185 status=SIMMultiplyImage(ncc_image,1.0/(QuantumScale*maxima),
4186 (
const ChannelStatistics *) NULL,exception);
4187 maxima=(double) QuantumRange;
4189 *similarity_metric=QuantumScale*maxima;
4193static Image *PhaseSimilarityImage(
const Image *image,
const Image *reconstruct,
4194 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4196#define ThrowPhaseSimilarityException() \
4198 if (correlation_image != (Image *) NULL) \
4199 correlation_image=DestroyImage(correlation_image); \
4200 if (fft_images != (Image *) NULL) \
4201 fft_images=DestroyImageList(fft_images); \
4202 if (gamma_image != (Image *) NULL) \
4203 gamma_image=DestroyImage(gamma_image); \
4204 if (magnitude_image != (Image *) NULL) \
4205 magnitude_image=DestroyImage(magnitude_image); \
4206 if (phase_image != (Image *) NULL) \
4207 phase_image=DestroyImage(phase_image); \
4208 if (reconstruct_image != (Image *) NULL) \
4209 reconstruct_image=DestroyImage(reconstruct_image); \
4210 if (reconstruct_magnitude != (Image *) NULL) \
4211 reconstruct_magnitude=DestroyImage(reconstruct_magnitude); \
4212 if (target_image != (Image *) NULL) \
4213 target_image=DestroyImage(target_image); \
4214 if (test_magnitude != (Image *) NULL) \
4215 test_magnitude=DestroyImage(test_magnitude); \
4216 return((Image *) NULL); \
4223 *correlation_image = (Image *) NULL,
4224 *fft_images = (Image *) NULL,
4225 *gamma_image = (Image *) NULL,
4226 *magnitude_image = (Image *) NULL,
4227 *phase_image = (Image *) NULL,
4228 *reconstruct_image = (Image *) NULL,
4229 *reconstruct_magnitude = (Image *) NULL,
4230 *target_image = (Image *) NULL,
4231 *test_magnitude = (Image *) NULL;
4234 status = MagickTrue;
4242 target_image=CloneImage(image,0,0,MagickTrue,exception);
4243 if (target_image == (Image *) NULL)
4244 ThrowPhaseSimilarityException();
4245 (void) ResetImagePage(target_image,
"0x0+0+0");
4246 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4247 &target_image->background_color);
4248 status=SetImageExtent(target_image,2*(
size_t) ceil((
double) image->columns/
4249 2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
4250 if (status == MagickFalse)
4251 ThrowPhaseSimilarityException();
4255 reconstruct_image=CloneImage(reconstruct,0,0,MagickTrue,exception);
4256 if (reconstruct_image == (Image *) NULL)
4257 ThrowPhaseSimilarityException();
4258 (void) ResetImagePage(reconstruct_image,
"0x0+0+0");
4259 GetPixelInfoRGBA((Quantum) 0,(Quantum) 0,(Quantum) 0,(Quantum) 0,
4260 &reconstruct_image->background_color);
4261 status=SetImageExtent(reconstruct_image,2*(
size_t) ceil((
double)
4262 image->columns/2.0),2*(
size_t) ceil((
double) image->rows/2.0),exception);
4263 if (status == MagickFalse)
4264 ThrowPhaseSimilarityException();
4268 (void) SetImageArtifact(target_image,
"fourier:normalize",
"inverse");
4269 fft_images=ForwardFourierTransformImage(target_image,MagickTrue,exception);
4270 if (fft_images == (Image *) NULL)
4271 ThrowPhaseSimilarityException();
4272 test_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4273 fft_images=DestroyImageList(fft_images);
4274 if (test_magnitude == (Image *) NULL)
4275 ThrowPhaseSimilarityException();
4276 (void) SetImageArtifact(reconstruct_image,
"fourier:normalize",
"inverse");
4277 fft_images=ForwardFourierTransformImage(reconstruct_image,MagickTrue,
4279 if (fft_images == (Image *) NULL)
4280 ThrowPhaseSimilarityException();
4281 reconstruct_magnitude=CloneImage(fft_images,0,0,MagickTrue,exception);
4282 fft_images=DestroyImageList(fft_images);
4283 if (reconstruct_magnitude == (Image *) NULL)
4284 ThrowPhaseSimilarityException();
4285 magnitude_image=CloneImage(reconstruct_magnitude,0,0,MagickTrue,exception);
4286 if (magnitude_image == (Image *) NULL)
4287 ThrowPhaseSimilarityException();
4288 DisableCompositeClampUnlessSpecified(magnitude_image);
4289 (void) CompositeImage(magnitude_image,test_magnitude,MultiplyCompositeOp,
4290 MagickTrue,0,0,exception);
4294 correlation_image=SIMPhaseCorrelationImage(target_image,reconstruct_image,
4295 magnitude_image,exception);
4296 target_image=DestroyImage(target_image);
4297 reconstruct_image=DestroyImage(reconstruct_image);
4298 test_magnitude=DestroyImage(test_magnitude);
4299 reconstruct_magnitude=DestroyImage(reconstruct_magnitude);
4300 if (correlation_image == (Image *) NULL)
4301 ThrowPhaseSimilarityException();
4305 gamma_image=CloneImage(correlation_image,0,0,MagickTrue,exception);
4306 correlation_image=DestroyImage(correlation_image);
4307 if (gamma_image == (Image *) NULL)
4308 ThrowPhaseSimilarityException();
4312 SetGeometry(image,&geometry);
4313 geometry.width=image->columns;
4314 geometry.height=image->rows;
4315 (void) ResetImagePage(gamma_image,
"0x0+0+0");
4316 phase_image=CropImage(gamma_image,&geometry,exception);
4317 gamma_image=DestroyImage(gamma_image);
4318 if (phase_image == (Image *) NULL)
4319 ThrowPhaseSimilarityException();
4320 (void) ResetImagePage(phase_image,
"0x0+0+0");
4324 status=GrayscaleImage(phase_image,AveragePixelIntensityMethod,exception);
4325 if (status == MagickFalse)
4326 ThrowPhaseSimilarityException();
4327 phase_image->depth=32;
4328 phase_image->colorspace=GRAYColorspace;
4329 phase_image->alpha_trait=UndefinedPixelTrait;
4330 status=SIMFilterImageNaNs(phase_image,exception);
4331 if (status == MagickFalse)
4332 ThrowPhaseSimilarityException();
4333 status=SIMMaximaImage(phase_image,&maxima,offset,exception);
4334 if (status == MagickFalse)
4335 ThrowPhaseSimilarityException();
4336 magnitude_image=DestroyImage(magnitude_image);
4337 if ((QuantumScale*maxima) > 1.0)
4339 status=SIMMultiplyImage(phase_image,1.0/(QuantumScale*maxima),
4340 (
const ChannelStatistics *) NULL,exception);
4341 maxima=(double) QuantumRange;
4343 *similarity_metric=QuantumScale*maxima;
4344 return(phase_image);
4347static Image *PSNRSimilarityImage(
const Image *image,
const Image *reconstruct,
4348 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4351 *psnr_image = (Image *) NULL;
4353 psnr_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4355 if (psnr_image == (Image *) NULL)
4357 *similarity_metric=10.0*MagickSafeLog10(MagickSafeReciprocal(
4358 *similarity_metric))/MagickSafePSNRRecipicol(10.0);
4362static Image *RMSESimilarityImage(
const Image *image,
const Image *reconstruct,
4363 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4366 *rmse_image = (Image *) NULL;
4368 rmse_image=MSESimilarityImage(image,reconstruct,offset,similarity_metric,
4370 if (rmse_image == (Image *) NULL)
4372 *similarity_metric=sqrt(*similarity_metric);
4377static double GetSimilarityMetric(
const Image *image,
4378 const Image *reconstruct_image,
const MetricType metric,
4379 const ssize_t x_offset,
const ssize_t y_offset,ExceptionInfo *exception)
4382 *channel_similarity,
4386 *sans_exception = AcquireExceptionInfo();
4392 status = MagickTrue;
4398 length = MaxPixelChannels+1UL;
4400 SetGeometry(reconstruct_image,&geometry);
4401 geometry.x=x_offset;
4402 geometry.y=y_offset;
4403 similarity_image=CropImage(image,&geometry,sans_exception);
4404 sans_exception=DestroyExceptionInfo(sans_exception);
4405 if (similarity_image == (Image *) NULL)
4410 channel_similarity=(
double *) AcquireQuantumMemory(length,
4411 sizeof(*channel_similarity));
4412 if (channel_similarity == (
double *) NULL)
4413 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
4414 (void) memset(channel_similarity,0,length*
sizeof(*channel_similarity));
4417 case AbsoluteErrorMetric:
4419 status=GetAESimilarity(similarity_image,reconstruct_image,
4420 channel_similarity,exception);
4423 case DotProductCorrelationErrorMetric:
4425 status=GetDPCSimilarity(similarity_image,reconstruct_image,
4426 channel_similarity,exception);
4429 case FuzzErrorMetric:
4431 status=GetFUZZSimilarity(similarity_image,reconstruct_image,
4432 channel_similarity,exception);
4435 case MeanAbsoluteErrorMetric:
4437 status=GetMAESimilarity(similarity_image,reconstruct_image,
4438 channel_similarity,exception);
4441 case MeanErrorPerPixelErrorMetric:
4443 status=GetMEPPSimilarity(similarity_image,reconstruct_image,
4444 channel_similarity,exception);
4447 case MeanSquaredErrorMetric:
4449 status=GetMSESimilarity(similarity_image,reconstruct_image,
4450 channel_similarity,exception);
4453 case NormalizedCrossCorrelationErrorMetric:
4455 status=GetNCCSimilarity(similarity_image,reconstruct_image,
4456 channel_similarity,exception);
4459 case PeakAbsoluteErrorMetric:
4461 status=GetPASimilarity(similarity_image,reconstruct_image,
4462 channel_similarity,exception);
4465 case PeakSignalToNoiseRatioErrorMetric:
4467 status=GetPSNRSimilarity(similarity_image,reconstruct_image,
4468 channel_similarity,exception);
4471 case PerceptualHashErrorMetric:
4473 status=GetPHASHSimilarity(similarity_image,reconstruct_image,
4474 channel_similarity,exception);
4477 case PhaseCorrelationErrorMetric:
4479 status=GetPHASESimilarity(similarity_image,reconstruct_image,
4480 channel_similarity,exception);
4483 case RootMeanSquaredErrorMetric:
4484 case UndefinedErrorMetric:
4487 status=GetRMSESimilarity(similarity_image,reconstruct_image,
4488 channel_similarity,exception);
4491 case StructuralDissimilarityErrorMetric:
4493 status=GetDSSIMSimilarity(similarity_image,reconstruct_image,
4494 channel_similarity,exception);
4497 case StructuralSimilarityErrorMetric:
4499 status=GetSSIMSimularity(similarity_image,reconstruct_image,
4500 channel_similarity,exception);
4504 similarity_image=DestroyImage(similarity_image);
4505 similarity=channel_similarity[CompositePixelChannel];
4506 channel_similarity=(
double *) RelinquishMagickMemory(channel_similarity);
4507 if (status == MagickFalse)
4512MagickExport Image *SimilarityImage(
const Image *image,
const Image *reconstruct,
4513 const MetricType metric,
const double similarity_threshold,
4514 RectangleInfo *offset,
double *similarity_metric,ExceptionInfo *exception)
4516#define SimilarityImageTag "Similarity/Image"
4532 *similarity_image = (Image *) NULL;
4535 status = MagickTrue;
4541 similarity_info = { 0.0, 0, 0 };
4546 assert(image != (
const Image *) NULL);
4547 assert(image->signature == MagickCoreSignature);
4548 assert(exception != (ExceptionInfo *) NULL);
4549 assert(exception->signature == MagickCoreSignature);
4550 assert(offset != (RectangleInfo *) NULL);
4551 if (IsEventLogging() != MagickFalse)
4552 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
4553 SetGeometry(reconstruct,offset);
4554 *similarity_metric=0.0;
4557#if defined(MAGICKCORE_HDRI_SUPPORT) && defined(MAGICKCORE_FFTW_DELEGATE)
4559 const char *artifact = GetImageArtifact(image,
"compare:frequency-domain");
4560 if (artifact == (
const char *) NULL)
4561 artifact=GetImageArtifact(image,
"compare:accelerate-ncc");
4562 if (((artifact == (
const char *) NULL) ||
4563 (IsStringTrue(artifact) != MagickFalse)) &&
4564 ((image->channels & ReadMaskChannel) == 0))
4567 case DotProductCorrelationErrorMetric:
4569 similarity_image=DPCSimilarityImage(image,reconstruct,offset,
4570 similarity_metric,exception);
4571 return(similarity_image);
4573 case MeanSquaredErrorMetric:
4575 similarity_image=MSESimilarityImage(image,reconstruct,offset,
4576 similarity_metric,exception);
4577 return(similarity_image);
4579 case NormalizedCrossCorrelationErrorMetric:
4581 similarity_image=NCCSimilarityImage(image,reconstruct,offset,
4582 similarity_metric,exception);
4583 return(similarity_image);
4585 case PeakSignalToNoiseRatioErrorMetric:
4587 similarity_image=PSNRSimilarityImage(image,reconstruct,offset,
4588 similarity_metric,exception);
4589 return(similarity_image);
4591 case PhaseCorrelationErrorMetric:
4593 similarity_image=PhaseSimilarityImage(image,reconstruct,offset,
4594 similarity_metric,exception);
4595 return(similarity_image);
4597 case RootMeanSquaredErrorMetric:
4598 case UndefinedErrorMetric:
4600 similarity_image=RMSESimilarityImage(image,reconstruct,offset,
4601 similarity_metric,exception);
4602 return(similarity_image);
4609 if ((image->columns < reconstruct->columns) ||
4610 (image->rows < reconstruct->rows))
4612 (void) ThrowMagickException(exception,GetMagickModule(),OptionWarning,
4613 "GeometryDoesNotContainImage",
"`%s'",image->filename);
4614 return((Image *) NULL);
4616 similarity_image=CloneImage(image,image->columns,image->rows,MagickTrue,
4618 if (similarity_image == (Image *) NULL)
4619 return((Image *) NULL);
4620 similarity_image->depth=32;
4621 similarity_image->colorspace=GRAYColorspace;
4622 similarity_image->alpha_trait=UndefinedPixelTrait;
4623 status=SetImageStorageClass(similarity_image,DirectClass,exception);
4624 if (status == MagickFalse)
4625 return(DestroyImage(similarity_image));
4629 similarity_info.similarity=GetSimilarityMetric(image,reconstruct,metric,
4630 similarity_info.x,similarity_info.y,exception);
4631 similarity_view=AcquireAuthenticCacheView(similarity_image,exception);
4632#if defined(MAGICKCORE_OPENMP_SUPPORT)
4633 #pragma omp parallel for schedule(static) shared(similarity_info,status) \
4634 magick_number_threads(image,reconstruct,similarity_image->rows,1)
4636 for (y=0; y < (ssize_t) similarity_image->rows; y++)
4642 threshold_trigger = MagickFalse;
4648 channel_info = similarity_info;
4653 if (status == MagickFalse)
4655 if (threshold_trigger != MagickFalse)
4657 q=QueueCacheViewAuthenticPixels(similarity_view,0,y,
4658 similarity_image->columns,1,exception);
4659 if (q == (Quantum *) NULL)
4664 for (x=0; x < (ssize_t) similarity_image->columns; x++)
4669 similarity=GetSimilarityMetric((Image *) image,reconstruct,metric,x,y,
4673 case DotProductCorrelationErrorMetric:
4674 case NormalizedCrossCorrelationErrorMetric:
4675 case PeakSignalToNoiseRatioErrorMetric:
4676 case PhaseCorrelationErrorMetric:
4677 case StructuralSimilarityErrorMetric:
4679 if (similarity <= channel_info.similarity)
4681 channel_info.similarity=similarity;
4688 if (similarity >= channel_info.similarity)
4690 channel_info.similarity=similarity;
4696 for (i=0; i < (ssize_t) GetPixelChannels(image); i++)
4698 PixelChannel channel = GetPixelChannelChannel(image,i);
4699 PixelTrait traits = GetPixelChannelTraits(image,channel);
4700 PixelTrait similarity_traits = GetPixelChannelTraits(similarity_image,
4702 if (((traits & UpdatePixelTrait) == 0) ||
4703 ((similarity_traits & UpdatePixelTrait) == 0))
4707 case DotProductCorrelationErrorMetric:
4708 case NormalizedCrossCorrelationErrorMetric:
4709 case PeakSignalToNoiseRatioErrorMetric:
4710 case PhaseCorrelationErrorMetric:
4711 case StructuralSimilarityErrorMetric:
4713 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4714 QuantumRange*similarity),q);
4719 SetPixelChannel(similarity_image,channel,ClampToQuantum((
double)
4720 QuantumRange*(1.0-similarity)),q);
4725 q+=(ptrdiff_t) GetPixelChannels(similarity_image);
4727#if defined(MAGICKCORE_OPENMP_SUPPORT)
4728 #pragma omp critical (MagickCore_GetSimilarityMetric)
4732 case DotProductCorrelationErrorMetric:
4733 case NormalizedCrossCorrelationErrorMetric:
4734 case PeakSignalToNoiseRatioErrorMetric:
4735 case PhaseCorrelationErrorMetric:
4736 case StructuralSimilarityErrorMetric:
4738 if (similarity_threshold != DefaultSimilarityThreshold)
4739 if (channel_info.similarity >= similarity_threshold)
4740 threshold_trigger=MagickTrue;
4741 if (channel_info.similarity >= similarity_info.similarity)
4742 similarity_info=channel_info;
4747 if (similarity_threshold != DefaultSimilarityThreshold)
4748 if (channel_info.similarity < similarity_threshold)
4749 threshold_trigger=MagickTrue;
4750 if (channel_info.similarity < similarity_info.similarity)
4751 similarity_info=channel_info;
4755 if (SyncCacheViewAuthenticPixels(similarity_view,exception) == MagickFalse)
4757 if (image->progress_monitor != (MagickProgressMonitor) NULL)
4763 proceed=SetImageProgress(image,SimilarityImageTag,progress,image->rows);
4764 if (proceed == MagickFalse)
4768 similarity_view=DestroyCacheView(similarity_view);
4769 if (status == MagickFalse)
4770 similarity_image=DestroyImage(similarity_image);
4771 *similarity_metric=similarity_info.similarity;
4772 if (fabs(*similarity_metric) < MagickEpsilon)
4773 *similarity_metric=0.0;
4774 offset->x=similarity_info.x;
4775 offset->y=similarity_info.y;
4776 (void) FormatImageProperty((Image *) image,
"similarity",
"%.*g",
4777 GetMagickPrecision(),*similarity_metric);
4778 (void) FormatImageProperty((Image *) image,
"similarity.offset.x",
"%.*g",
4779 GetMagickPrecision(),(
double) offset->x);
4780 (void) FormatImageProperty((Image *) image,
"similarity.offset.y",
"%.*g",
4781 GetMagickPrecision(),(
double) offset->y);
4782 return(similarity_image);