Can't figure out changing from B&W to color in slitscanner


#1

I’m trying to take a sample row of pixels from the top of my video monitor and repeating the sample coming up from the bottom of a separate display screen. When I started the project and it was sampling a vertical row of pixels and repeating the sample left to right, the colors were there. As soon as I switched from vertical to horizontal, the colors on the display went to black and white.

I started with the code from this website to create a basic slitscanner: https://www.instructables.com/id/Experiments-with-slit-scan-photography/

Here is the part of my code that I think the issue may be in:
void ofApp::movePixelsInImage(ofImage * image) {
unsigned char * pixels = image->getPixels().getData(); //equivalent to (image).getPixels();
for (int col=0; colgetWidth()-1; col++) { //Was X
for (int row=0; rowgetHeight(); row++) { //Was y
int position = row
(image->getWidth()3)+col3;
int nextPosition = position+camWidth*3;
pixels[position] = pixels[nextPosition];
pixels[position+1] = pixels[nextPosition+1];
pixels[position+2] = pixels[nextPosition+2];
}
}
image->update();
}


#2

Hi,
Welcome to the OF forum!
can you put the code that is working and the one that is not, as well as some screenshots with the results please.
Also, please, when posting code, in the text editor, select it all and press the </> button that is in the tool bar above.
It will be a lot easier to read.

cheers


#3

Here are the images of my screen. They’re sideways because of the sizing of the images. The displayed image on the left is really on the top of my screen, and the display with the red is on the bottom of my screen. Ideally, the program will take a line of pixels from the top row of the top screen (which is my video camera monitor) and output them on the bottom screen from bottom to top.

I’m not sure which code is not working, since the program runs now without issues. Here is my code below!
Thank you for your help!

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

    ofBackground(0,0,0);
    
    // try to grab at this size
    camWidth = 640;
    camHeight = 480;
    //slitScanWidth   = 1000; //width of the slit scan image
    slitScanHeight = 1000;
    //slitScanGrabColumn = 240; //grab pixels with x=240 (i.e. the center column of the webcam image)
    slitScanGrabRow = 240;
    
    vidGrabber.setup(camHeight,camWidth);
    //change to camHeight, camWidtch
    vidGrabber.setDesiredFrameRate(90);
    vidGrabber.initGrabber(camHeight,camWidth);
    //change to camHeight, camWidtch
    slitScanImage.allocate(camWidth, camHeight, OF_IMAGE_COLOR);
    //changed to slitScanHeight
    ofSetVerticalSync(true);
    

    ofEnableAlphaBlending();
    
}

//--------------------------------------------------------------
void ofApp::update(){

    vidGrabber.update();
    if (vidGrabber.isFrameNew()){
        unsigned char * videoPixels = vidGrabber.getPixels().getData();
        unsigned char * imagePixels = slitScanImage.getPixels().getData();
        
        movePixelsInImage(&slitScanImage);
        
        for (int x = 0; x < camWidth; x++) {
            //change to camWidth, x++
            int imagePosition = (camHeight*3)+(x)*3;
            int videoPosition = (x)*3;
        
            imagePixels[imagePosition]   = videoPixels[videoPosition];
            imagePixels[imagePosition+1] = videoPixels[videoPosition+1];
            imagePixels[imagePosition+2] = videoPixels[videoPosition+2];
        }
        
        slitScanImage.update();
        
    }
}

void ofApp::movePixelsInImage(ofImage * image) {
    unsigned char * pixels = image->getPixels().getData(); //equivalent to (*image).getPixels();
    for (int col=0; col<image->getWidth()-1; col++) { //Was X
        for (int row=0; row<image->getHeight(); row++) { //Was y
            int position = row*(image->getWidth()*3)+col*3;
            int nextPosition = position+camWidth*3;
            pixels[position] = pixels[nextPosition];
            pixels[position+1] = pixels[nextPosition+1];
            pixels[position+2] = pixels[nextPosition+2];
            pixels[position+3] = pixels[nextPosition+3]; //makes red
            //pixels[position+4] = pixels[nextPosition+4]; made yellow

        }
    }
    image->update();
}

//--------------------------------------------------------------
void ofApp::draw(){

    ofSetHexColor(0xffffff);
    vidGrabber.draw(20,20);
    ofPixelsRef pixelsRef = vidGrabber.getPixels();
    slitScanImage.draw(20, camHeight+40);
    //ofLine(20+slitScanGrabColumn,20,20+slitScanGrabColumn,20+camHeight);
    
    for (int i = 0; i < camWidth; i+= 7){
        for (int j = 0; j < camHeight; j+= 9){
            // get the pixel and its lightness (lightness is the average of its RGB values)
            //float lightness = pixelsRef.getColor(i,j).getLightness();
            
           
}
    }}