2016-06-11 1 views
2

이미지의 각 픽셀을 데이터 프레임의 변수로 변환하는 솔루션을 찾고 있습니다.픽셀 매트릭스를 변수 (데이터 프레임)로 변환 R

해상도가 320x280px 인 ~ 2500 개의 입력 이미지가 있고 readJPEG() 인 매트릭스로 읽습니다. 파일 이름에는 나중에 분류해야하는 변수에 대한 정보가 들어 있습니다.

file_list <- list.files("D:/path/to/images", full.names=TRUE) 
# Extract person number and eye position from file names 
file_names <- sapply(strsplit(file_list, split = '/'), "[", 8) 
person_list <- substr(file_names, 1 ,3) 
person_class <- as.factor(person_list) 

# Read pixel matrices from image files 
pixelMatrices = lapply(X=file_list, FUN= function(x) readJPEG(file_list)) 
entryCount <- length(file_list) 

# Setting up a proper data set 
eyes = data.frame(pos= numeric(entryCount)) 
eyes$person <- person_class 
eyes$pixels <- pixelMatrices 

이렇게하면 각 개체에 2 개의 변수 (사람, 픽셀)가있는 데이터 프레임이 생성됩니다. 하지만 320 * 280 + 1 변수가있는 데이터 프레임이 필요합니다. 각 픽셀 및 요소 클래스에 대해 하나.

제가

매트릭스
test <- as.data.frame(x = unlist(pixelMatrices[1])) 
test <- unlist(pixelMatrices[1]) 
test <- as.data.frame(pixelMatrices[1]) 

를 목록에서 제외하지만 비 적절한 결과를 제공 같은 다른 방법을 시도했다.

count <- length(file_list) 
imageWidth = 320; 
imageHeight = 280; 
variableCount = imageHeight * imageWidth + 1 

images <- as.data.frame(matrix(seq(count),nrow=count,ncol=variableCount)) 
images[1] <- eyes$person 
for(i in 1:count) { 
    img <- readJPEG(file_list[i]) 
    image <- c(img) 
    images[i, 2:variableCount] <- image 
} 

그러나 루프에 대한 매우 느린 : 내가 지금까지 가지고있는 유일한 (거의) 작업 방법은 모든 픽셀 이상의 루프 삽입 행 행에 의하여 다음과 같습니다 데이터 세트에 대한 것입니다. 그래서 ~ 2500 obj로 결과 데이터 프레임을 얻는 최선의 방법은 무엇입니까? 89601 변수 중?

답변

1

에서 행렬을 병합하는 것이 person_class를 pixelMatrices의 변환 된 각 데이터 프레임에 반복적으로 호출하는 것을 고려하십시오. 그런 다음 do.call()을 실행하여 최종 데이터 프레임에 바인드합니다. Mapply는 person_class의 각 요소에 연결된 매트릭스에 정렬됩니다 보장 :

combinedata <- function(x,y){ 
        # FLATTEN MATRIX AND TRANSPOSE ACROSS MANY COLUMNS 
        temp <- data.frame(t(as.vector(x))) 
        temp$person_class <- y 
        return(temp) 
       } 

# WIDE LIST 
dfList <- mapply(combinedata, pixelMatrices, person_class) 

# LONG LIST CONVERSION FOR DO.CALL() 
dfList <- lapply(1:entryCount, function(i) data.frame(dfList[,i])) 

# ROW BIND UNDERLYING DATA FRAMES 
finaldf <- do.call(rbind, dfList) 
+0

나는 당신의 접근 방식을 사용하여 시도하지만 내 RStudio는 계산 라인의 몇 분 후에 나에게 다음과 같은 오류를 준'dfList <- mapply (combinedata, pixelMatrices, person_class) ': R Session Aborted, R에 치명적인 오류가 발생했습니다. 세션이 종료되었습니다. – 4ndro1d

+0

왜 많은 열이 필요한지 물어볼 수 있습니까? 행렬을 병합하지 않고 dfs로 변환하고 각 행마다 person_class를 반복하면 어떨까요? – Parfait

+0

나중에 가장 중요한 기능을 찾으려면 PCA를 사용하고 싶습니다. 내 모델을 훈련시키기 위해 여러 데이터 프레임을 어떻게 사용하는지 잘 모르겠습니다. 나는 R의 초보자이며, 멍청한 튜토리얼을 가지고 있지 않기 때문에 꽤 제한되어있다. – 4ndro1d

관련 문제