10000 Encapsulate freeimage usage from pycolmap in colmap bitmap by ahojnnes · Pull Request #2372 · colmap/colmap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Encapsulate freeimage usage from pycolmap in colmap bitmap #2372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pycolmap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ find_package(pybind11 2.11.1 REQUIRED)

pybind11_add_module(pycolmap ${PROJECT_SOURCE_DIR}/../src/pycolmap/main.cc)
target_include_directories(pycolmap PRIVATE ${PROJECT_SOURCE_DIR}/../src/)
target_link_libraries(pycolmap PRIVATE colmap::colmap freeimage::FreeImage glog::glog Ceres::ceres)
target_link_libraries(pycolmap PRIVATE colmap::colmap glog::glog Ceres::ceres)
target_compile_definitions(pycolmap PRIVATE VERSION_INFO="${PROJECT_VERSION}")
install(TARGETS pycolmap LIBRARY DESTINATION .)
2 changes: 1 addition & 1 deletion src/colmap/feature/sift.cc
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ class SiftGPUFeatureExtractor : public FeatureExtractor {
// Note, that this produces slightly different results than using SiftGPU
// directly for RGB->GRAY conversion, since it uses different weights.
const std::vector<uint8_t> bitmap_raw_bits = bitmap.ConvertToRawBits();
const int code = sift_gpu_.RunSIFT(bitmap.ScanWidth(),
const int code = sift_gpu_.RunSIFT(bitmap.Pitch(),
bitmap.Height(),
bitmap_raw_bits.data(),
GL_LUMINANCE,
Expand Down
53 changes: 33 additions & 20 deletions src/colmap/sensor/bitmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void Bitmap::Deallocate() {

size_t Bitmap::NumBytes() const {
if (handle_.ptr != nullptr) {
return ScanWidth() * height_;
return Pitch() * height_;
} else {
return 0;
}
Expand All @@ -170,25 +170,7 @@ unsigned int Bitmap::BitsPerPixel() const {
return FreeImage_GetBPP(handle_.ptr);
}

unsigned int Bitmap::ScanWidth() const {
return FreeImage_GetPitch(handle_.ptr);
}

std::vector<uint8_t> Bitmap::ConvertToRawBits() const {
const unsigned int scan_width = ScanWidth();
const unsigned int bpp = BitsPerPixel();
const bool kTopDown = true;
std::vector<uint8_t> raw_bits(scan_width * height_, 0);
FreeImage_ConvertToRawBits(raw_bits.data(),
handle_.ptr,
scan_width,
bpp,
FI_RGBA_RED_MASK,
FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK,
kTopDown);
return raw_bits;
}
unsigned int Bitmap::Pitch() const { return FreeImage_GetPitch(handle_.ptr); }

std::vector<uint8_t> Bitmap::ConvertToRowMajorArray() const {
std::vector<uint8_t> array(width_ * height_ * channels_);
Expand Down Expand Up @@ -221,6 +203,37 @@ std::vector<uint8_t> Bitmap::ConvertToColMajorArray() const {
return array;
}

std::vector<uint8_t> Bitmap::ConvertToRawBits() const {
const unsigned int pitch = Pitch();
const unsigned int bpp = BitsPerPixel();
std::vector<uint8_t> raw_bits(pitch * height_ * bpp / 8, 0);
FreeImage_ConvertToRawBits(raw_bits.data(),
handle_.ptr,
pitch,
bpp,
FI_RGBA_RED_MASK,
FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK,
/*topdown=*/true);
return raw_bits;
}

Bitmap Bitmap::ConvertFromRawBits(
const uint8_t* data, int pitch, int width, int height, bool rgb) {
const unsigned bpp = rgb ? 24 : 8;
return Bitmap(FreeImage_ConvertFromRawBitsEx(/*copy_source=*/true,
const_cast<uint8_t*>(data),
FIT_BITMAP,
width,
height,
pitch,
bpp,
FI_RGBA_RED_MASK,
FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK,
/*topdown=*/true));
}

bool Bitmap::GetPixel(const int x,
const int y,
BitmapColor<uint8_t>* color) const {
Expand Down
10 changes: 7 additions & 3 deletions src/colmap/sensor/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class Bitmap {
unsigned int BitsPerPixel() const;

// Scan width of bitmap which differs from the actual image width to achieve
// 32 bit aligned memory. Also known as pitch or stride.
unsigned int ScanWidth() const;
// 32 bit aligned memory. Also known as stride.
unsigned int Pitch() const;

// Check whether image is grey- or colorscale.
inline bool IsRGB() const;
Expand All @@ -114,10 +114,14 @@ class Bitmap {
size_t NumBytes() const;

// Copy raw image data to array.
std::vector<uint8_t> ConvertToRawBits() const;
std::vector<uint8_t> ConvertToRowMajorArray() const;
std::vector<uint8_t> ConvertToColMajorArray() const;

// Convert to/from raw bits.
std::vector<uint8_t> ConvertToRawBits() const;
static Bitmap ConvertFromRawBits(
const uint8_t* data, int pitch, int width, int height, bool rgb = true);

// Manipulate individual pixels. For grayscale images, only the red element
// of the RGB color is used.
bool GetPixel(int x, int y, BitmapColor<uint8_t>* color) const;
Expand Down
62 changes: 58 additions & 4 deletions src/colmap/sensor/bitmap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ TEST(Bitmap, ConvertToRowMajorArrayRGB) {
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2, 0, 0));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3, 0, 0));
const std::vector<uint8_t> array = bitmap.ConvertToRowMajorArray();
EXPECT_EQ(array.size(), 12);
ASSERT_EQ(array.size(), 12);
EXPECT_EQ(array[0], 0);
EXPECT_EQ(array[1], 0);
EXPECT_EQ(array[2], 0);
Expand All @@ -186,7 +186,7 @@ TEST(Bitmap, ConvertToRowMajorArrayGrey) {
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2, 0, 0));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3, 0, 0));
const std::vector<uint8_t> array = bitmap.ConvertToRowMajorArray();
EXPECT_EQ(array.size(), 4);
ASSERT_EQ(array.size(), 4);
EXPECT_EQ(array[0], 0);
EXPECT_EQ(array[1], 2);
EXPECT_EQ(array[2], 1);
Expand All @@ -201,7 +201,7 @@ TEST(Bitmap, ConvertToColMajorArrayRGB) {
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2, 0, 0));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3, 0, 0));
const std::vector<uint8_t> array = bitmap.ConvertToColMajorArray();
EXPECT_EQ(array.size(), 12);
ASSERT_EQ(array.size(), 12);
EXPECT_EQ(array[0], 0);
EXPECT_EQ(array[1], 0);
EXPECT_EQ(array[2], 0);
Expand All @@ -224,13 +224,67 @@ TEST(Bitmap, ConvertToColMajorArrayGrey) {
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2, 0, 0));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3, 0, 0));
const std::vector<uint8_t> array = bitmap.ConvertToColMajorArray();
EXPECT_EQ(array.size(), 4);
ASSERT_EQ(array.size(), 4);
EXPECT_EQ(array[0], 0);
EXPECT_EQ(array[1], 1);
EXPECT_EQ(array[2], 2);
EXPECT_EQ(array[3], 3);
}

TEST(Bitmap, ConvertToFromRawBitsGrey) {
Bitmap bitmap;
bitmap.Allocate(3, 2, false);
bitmap.SetPixel(0, 0, BitmapColor<uint8_t>(0));
bitmap.SetPixel(0, 1, BitmapColor<uint8_t>(1));
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3));

std::vector<uint8_t> raw_bits = bitmap.ConvertToRawBits();
ASSERT_EQ(raw_bits.size(), bitmap.Pitch() * bitmap.Height());

const std::vector<uint8_t> raw_bits_copy = raw_bits;
Bitmap bitmap_copy = Bitmap::ConvertFromRawBits(raw_bits.data(),
bitmap.Pitch(),
bitmap.Width(),
bitmap.Height(),
/*rgb=*/false);
EXPECT_EQ(bitmap.Width(), bitmap_copy.Width());
EXPECT_EQ(bitmap.Height(), bitmap_copy.Height());
EXPECT_EQ(bitmap.Channels(), bitmap_copy.Channels());
bitmap.SetPixel(0, 1, BitmapColor<uint8_t>(5));
bitmap_copy.SetPixel(0, 1, BitmapColor<uint8_t>(5));
EXPECT_EQ(raw_bits_copy, raw_bits);
EXPECT_EQ(bitmap.ConvertToRowMajorArray(),
bitmap_copy.ConvertToRowMajorArray());
}

TEST(Bitmap, ConvertToFromRawBitsRGB) {
Bitmap bitmap;
bitmap.Allocate(3, 2, true);
bitmap.SetPixel(0, 0, BitmapColor<uint8_t>(0, 0, 0));
bitmap.SetPixel(0, 1, BitmapColor<uint8_t>(1, 0, 0));
bitmap.SetPixel(1, 0, BitmapColor<uint8_t>(2, 0, 0));
bitmap.SetPixel(1, 1, BitmapColor<uint8_t>(3, 0, 0));

std::vector<uint8_t> raw_bits = bitmap.ConvertToRawBits();
ASSERT_EQ(raw_bits.size(), bitmap.Pitch() * bitmap.Height() * 3);

const std::vector<uint8_t> raw_bits_copy = raw_bits;
Bitmap bitmap_copy = Bitmap::ConvertFromRawBits(raw_bits.data(),
bitmap.Pitch(),
bitmap.Width(),
bitmap.Height(),
/*rgb=*/true);
EXPECT_EQ(bitmap.Width(), bitmap_copy.Width());
EXPECT_EQ(bitmap.Height(), bitmap_copy.Height());
EXPECT_EQ(bitmap.Channels(), bitmap_copy.Channels());
bitmap.SetPixel(0, 1, BitmapColor<uint8_t>(5, 0, 0));
bitmap_copy.SetPixel(0, 1, BitmapColor<uint8_t>(5, 0, 0));
EXPECT_EQ(raw_bits_copy, raw_bits);
EXPECT_EQ(bitmap.ConvertToRowMajorArray(),
bitmap_copy.ConvertToRowMajorArray());
}

TEST(Bitmap, GetAndSetPixelRGB) {
Bitmap bitmap;
bitmap.Allocate(1, 1, true);
Expand Down
24 changes: 6 additions & 18 deletions src/pycolmap/feature/sift.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <memory>

#include <Eigen/Core>
#include <FreeImage.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>

Expand Down Expand Up @@ -44,23 +43,12 @@ class Sift {
THROW_CHECK_LE(image.rows(), options_.max_image_size);
THROW_CHECK_LE(image.cols(), options_.max_image_size);

const unsigned int bpp = 8; // Grey.
const unsigned int width = image.cols();
const unsigned int scan_width = (bpp / 8) * width;
pyimage_t<uint8_t> image_copy = image;
FIBITMAP* bitmap_raw = FreeImage_ConvertFromRawBitsEx(
/*copySource=*/false,
static_cast<unsigned char*>(image_copy.data()),
FIT_BITMAP,
width,
image.rows(),
scan_width,
bpp,
FI_RGBA_RED_MASK,
FI_RGBA_GREEN_MASK,
FI_RGBA_BLUE_MASK,
/*topdown=*/true);
const Bitmap bitmap(bitmap_raw);
const Bitmap bitmap =
Bitmap::ConvertFromRawBits(const_cast<uint8_t*>(image.data()),
/*pitch=*/image.cols(),
/*width=*/image.cols(),
/*height=*/image.rows(),
/*rgb=*/false);

FeatureKeypoints keypoints_;
FeatureDescriptors descriptors_;
Expand Down
0