Cropping an openCV matrix to ROI gives me a glitchy image

ofApp.h

#pragma once

#include "ofMain.h"
#include "ofxOpenCv.h"
#include "ofxCv.h"

class ofApp : public ofBaseApp{
    
    ofVideoPlayer recordedDepthPlayback;
    ofImage ofDepthGridTest;
    ofImage ofDepthImg;

    
	public:
    
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
		
};

ofApp.cpp

#include "ofApp.h"
using namespace cv;
using namespace ofxCv;

//--------------------------------------------------------------
void ofApp::setup(){
    recordedDepthPlayback.load("kinect-depth-test.mov");
    recordedDepthPlayback.play();
    
    //of screen stuff
    ofSetFrameRate(30);
    ofSetVerticalSync(true);
    ofBackground(0);
    ofEnableAlphaBlending();
}

//--------------------------------------------------------------
void ofApp::update(){
    ofBackground(100, 100, 100);
    recordedDepthPlayback.update();
    if (recordedDepthPlayback.isFrameNew()) {
    
        cv::Mat depthImg;
        //read in depth image
        cv::Mat bgr = cv::Mat(recordedDepthPlayback.getHeight(), recordedDepthPlayback.getWidth(), CV_8UC3, recordedDepthPlayback.getPixels(), 0);
        //force back to 1 channel
        cv::cvtColor(bgr, depthImg, CV_BGR2GRAY);
        toOf(bgr, ofDepthImg);
        ofDepthImg.update();
        cv::Mat grid;
        cv::Rect roi = cv::Rect(200, 200, 200, 200);
        grid = depthImg(roi);
        toOf(grid, ofDepthGridTest);
        ofDepthGridTest.update();
    }

}

//--------------------------------------------------------------
void ofApp::draw(){
    ofDepthImg.draw(0,0);
    ofDepthGridTest.draw(640,0);
}

When I try to work with a cropped region of my recorded depth image I get this weird glitching effect. What gives? How can I avoid this? Even when I don’t force the recorded video back to grayscale (to emulate the real feed from the kinect) I still get the same glitching.

I’m not sure if it’s the same issue than yours but I ran into a similar problem, you could try using copy() instead of toOf() since it will notice if the destination is a different depth and do the conversion. I’ve tried to copy to an ofImage first but still had the problem but when copied to an ofPixels and it was ok.

ofImage img;
ofPixels px;
copy(yourMatHere, px);
img.setFromPixels(px);