1. 程式人生 > >R語言實現影象查重

R語言實現影象查重

(商業目的引用該文章請聯絡我,個人部落格引用該文章請註明來源,謝謝)

通過三個指令碼實現影象查重(需要提前匯入R的jpeg庫)

R指令碼路徑:

D:\Computer Science\Programming\R\ImageProcessing

影象儲存路徑:

D:\Computer Science\Programming\R\ImageProcessing\data

1_ImagePoint.r(該指令碼實現在所有影象中找點的動作)

setwd("D:/Computer Science/Programming/R/ImageProcessing/data")
print("working directory has been switched to: D:/Computer Science/Programming/R/ImageProcessing/data")

v <- list.files(getwd())
print("____________")
print("pic name:")
print(v)
print("____________")

num = length(v)
print("pic count:")
print(num)
print("____________")

library(jpeg)

orgpic=readJPEG(v[1])
v4res <- dim(orgpic)
a = v4res[1]
b = v4res[2]

for(i in 1:num) {
orgpic=readJPEG(v[i])
v4res <- dim(orgpic)
	if(a>v4res[1]){
	a=v4res[1]
	}
	if(b>v4res[2]){
	b=v4res[2]
	}
}

res4a <- sample(1:a, size = num)
res4b <- sample(1:b, size = num)

rownames = c("point")
colnames = c("X", "Y")

for(i in 1:num) {
M <- matrix(c(res4a[i],res4b[i]), ncol = 2,byrow = TRUE, dimnames = list(rownames, colnames))
print(M)
}
print("____________")

paintfun <- function(aa,bb){
plot(x = res4a,y = res4b,
   xlab = "Image_X",
   ylab = "Image_Y",
   xlim = c(1,aa),
   ylim = c(1,bb),		 
   main = "Discrete Point"
)
}

2_ImageCalculation.r(該指令碼實現點的RGB計算動作)

rgbsum <- c(0,0)
resget <- c(0,0)

for(i in 1:num) {
print(i)
a = 0
orgpic=readJPEG(v[i])
		for(j in 1:num) {
		v4rgb <- orgpic[res4a[j],res4b[j],]
		print(v4rgb)
		rgbsum[j] = (v4rgb[1]+v4rgb[2]+v4rgb[3])/3
		print (rgbsum[j])
		print("__")
		a = a+rgbsum[j]
		}
print(a)
resget[i]=a
print("____________")
}

picfun <- function(x){
  for(i in 1:x) {
	print(resget[i])
  }
}

picfun(num)
print("____________")

3_ImageMerging.r(該指令碼實現點的歸併及相同影象的檔名輸出)

c <- table(unlist(resget))
print(c)

c<-1:num

mergfun <- function(){
	for(i in c){
		if(!i%in%c)
		{
			next
		}
		#當前點的RGB的平均數,在RGB向量裡面相同元素的下標
		cnew <- which(resget==resget[i])
		c<-c[!c%in%cnew]
		
		print(v[cnew])
	}
}
mergfun()