带状矩阵的压缩
#include<opencv2/opencv.hpp>
int getRows(cv::Mat mat) {
if (mat.cols != mat.rows) {
return -1;
}
int myRow = 0;
cv::Mat matt = mat.t();
double* first_row = mat.ptr<double>(0);
double* first_col = matt.ptr<double>(0);
for (int i = 0; i < mat.cols; i++) {
if (first_row[i] != 0) {
myRow++;
}
if (first_col[i] != 0) {
myRow++;
}
}
return --myRow;
}
cv::Mat compressedBand(cv::Mat mat) {
cv::Mat matt = mat.t();
cv::Mat band = cv::Mat::zeros(cv::Size(mat.rows, getRows(mat)), mat.type());
double* band_row = band.ptr<double>(band.rows - 1);
for (int i = mat.cols - 1; i >= 0; i--) {
if (mat.at<double>(i, 0) != 0) {
for (int j = 0; j < mat.rows - i; j++) {
band_row[j] = mat.at<double>(i + j, 0 + j);
}
band_row -= band.cols;
}
}
for (int i = 1; i < mat.cols; i++) {
if (mat.at<double>(0, i) != 0) {
for (int j = i; j < mat.rows; j++) {
band_row[j] = mat.at<double>(j - i, j);
}
if (i != mat.cols - 1) {
band_row -= band.cols;
}
}
}
return band;
}
cv::Mat paraMul(cv::Mat a, cv::Mat b) {
cv::Mat product;
return product;
}
int main()
{
double a[7][7] = {
{ 3.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },
{ 4.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 },
{ 9.0, 2.0, 6.0, 5.0, 0.0, 0.0, 0.0 },
{ 0.0, 3.0, 5.0, 8.0, 9.0, 0.0, 0.0 },
{ 0.0, 0.0, 7.0, 9.0, 3.0, 2.0, 0.0 },
{ 0.0, 0.0, 0.0, 3.0, 8.0, 4.0, 6.0 },
{ 0.0, 0.0, 0.0, 0.0, 2.0, 4.0, 4.0 }
};
cv::Mat mat(7, 7, CV_64F, a);
std::cout << compressedBand(mat) << std::endl;
return 0;
}