根据像素值匹配R中的图像

zzz*_*zzz 2 r image-processing

function(){

img1<-readImage("image.jpg") 


img2<-img1+0.2

img1.b<-sum(img1[,,1:3])
img1.max<-length(img1[,,1:3])

diff1<-img1.b/img1.max

toincr<-increment(diff1)
img4<-img1
img4[,,1:3]<-img1[,,1:3]+toincr


img2.b<-sum(img2[,,1:3])

img2.max<-length(img2[,,1:3])
diff2<-img2.b/img2.max
toincr<-increment(diff2)
img3<-img2
img3[,,1:3]<-img2[,,1:3]+toincr


if(length(img3)==length(img4))
{
    paste(100*sum(img3==img4)/length(img3),"%")
}

}

function(diff)
{
brightness<-0.25
incr<-brightness-diff
return(incr)
}
Run Code Online (Sandbox Code Playgroud)

我已经写了上面的测试功能,它读取一个图像,然后创建一个副本并照亮该复制图像.然后我调整两个图像的亮度,使它们同样亮0.25值,即图像img3和img4.当我比较这两个图像使用然后显示结果为0%,但预期结果是100%.我应该如何修改程序?

Tro*_*roy 7

这里有png图书馆

require(png)
img1<-readPNG("apple1.png")
img2<-readPNG("apple2.png")

if(length(img1)==length(img2)){   # check same pixel number
  paste(100*sum(img1==img2)/length(img1),"%")
}

#[1] "32.098125 %"
Run Code Online (Sandbox Code Playgroud)

它比较了2个数组,为您提供了一个BOOLEAN匹配数组.sum()给出了匹配像素的数量.除以总像素(数组的长度())给出匹配因子(*100表示​​百分比)

好的,所以回答编辑后的评论!

首先,一个警告.图像比较和变化检测领域非常复杂,并且已经进行了大量的商业工作,使用商业包装而不是重新发明轮子可能会更好.

这里有一篇关于入侵者检测算法的好文章:http://ijcnis.org/index.php/ijcnis/article/viewFile/88/87

然而,问题是关于R并且它处理有用的概念,所以这里是一个(公认的简单化)答案:

当您查看连续帧时,您需要记住,在正常分辨率下,光线和曝光设置的微小变化将使您的图像完全不同,以便进行基本的算术计算.您还需要注意,由于振动或其他因素(例如,对于15度视角相机在400x400处为~1/10000度)的微小移动可能会使像素不对齐,因此即使亮度也无法进行比较调整算法)

所以要做到这一点,你需要做两件事:
1)解析图像(即将像素聚合到更少的块)2)将亮度分数加入桶中,使得按照正常移位的值的微小变化不会给你虚假信号.

试试这个:

例如,有2个样本图像:

空房间

在此输入图像描述

侵入者

在此输入图像描述

require(png)

img.reference<-readPNG("room-empty.png")     # empty room as reference
img.empty<-readPNG("room-empty.png")         # empty room copy
img.person<-readPNG("room-with-person.png")  # room with person in it

# function to de-resolve image into levels (n=granularity)
# and divide the image up into blocks (n=result.length)
chunkImage<-function(image,granularity=10,result.length=100){

  img.1D<-(image[,,1]+image[,,2]+image[,,1])/3
  pix.n<-length(img.1D)
  groups<-rep(1:result.length,each=ceiling(pix.n/result.length))[1:pix.n]
  imgmap.new<-aggregate(as.vector(img.1D),list(newpix=groups),mean)
  return(as.numeric(cut(imgmap.new$x,c(0:granularity)/granularity)))

}

# this returns an array (of reduced granularity) which describes each image in simpler terms
# you can think of it as a de-resolution or grouping function

chunkImage(img.reference)
#[1] 4 4 4 4 4 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6 6 7 7 7 7 6 5 5 5 6 6 5 5 5 5
#[52] 5 5 5 5 5 5 5 5 5 4 4 4 5 4 4 3 3 3 3 3 3 4 5 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4

# so to compare an empty room against reference
sum(chunkImage(img.empty)!=chunkImage(img.reference))
#[1] 0
# Score 0 so probably nothing to worry about

# now to compare with the image containing the person
sum(chunkImage(img.person)!=chunkImage(img.reference))
#[1] 14
# score 14 may indicate a person
Run Code Online (Sandbox Code Playgroud)

您必须根据站点和环境的具体情况修改分辨率和粒度.

PS:这里是解析图像的可视化表示,向您展示它们在视觉上如何被视为不同;

m.p<-matrix(chunkImage(img.person),ncol=10)/10
m.e<-matrix(chunkImage(img.empty),ncol=10)/10

m.p.big<-matrix(sapply(apply(m.p,1,function(x)rep(x,40)),function(x)rep(x,40)),ncol=400,byrow=T)
m.e.big<-matrix(sapply(apply(m.e,1,function(x)rep(x,40)),function(x)rep(x,40)),ncol=400,byrow=T)
m.alpha<-matrix(rep(1,160000),ncol=400)

length(m.e.big)

writePNG(array(c(rep(m.p.big,3),m.alpha),dim=c(400,400,4)),"person-deresolved.png")
writePNG(array(c(rep(m.e.big,3),m.alpha),dim=c(400,400,4)),"empty-deresolved.png")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述