Skip to content

Commit

Permalink
use oepncv to reduce save time.
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Mar 24, 2024
1 parent 2355b2a commit 571f9c9
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ include_directories(${ncnn_DIR}/${ANDROID_ABI}/include/ncnn)
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION
${ncnn_DIR}/${ANDROID_ABI}/lib/libncnn.so)

set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/OpenCV-android-sdk/sdk/native/jni/)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIR}/include)

#include_directories(${CMAKE_CURRENT_BINARY_DIR})
option(USE_SYSTEM_WEBP "build with system libwebp" OFF)
Expand Down Expand Up @@ -44,4 +47,4 @@ endif()

add_executable(realcugan-ncnn main.cpp realcugan.cpp)

target_link_libraries(realcugan-ncnn webp ncnn)
target_link_libraries(realcugan-ncnn webp ncnn ${OpenCV_LIBS})
29 changes: 28 additions & 1 deletion RealSR-NCNN-Android-CLI/RealCUGAN/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static std::vector<int> parse_optarg_int_array(const char* optarg)
#include "realcugan.h"

#include "filesystem_utils.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/hal/interface.h>
using namespace cv;

static void print_usage()
{
Expand Down Expand Up @@ -389,7 +392,31 @@ void* save(void* args)

path_t ext = get_file_extension(v.outpath);

if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
if (ext != PATHSTR("gif")) {
// 使用opencv保存图片,速度比默认的stb更快
cv::Mat image;
switch (v.outimage.elempack) {
case 1:
image = cv::Mat( v.outimage.h, v.outimage.w, CV_8UC1, v.outimage.data); // 单通道图像
break;
case 3:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC3, v.outimage.data); // 3通道图像
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
break;
case 4:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC4, v.outimage.data); // 4通道图像
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
break;
}
if (image.empty()) {
std::cerr << "Error: Image data not loaded." << std::endl;
success = false;
} else {
success = imwrite(v.outpath.c_str(), image);
}


}else if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
{
success = webp_save(v.outpath.c_str(), v.outimage.w, v.outimage.h, v.outimage.elempack, (const unsigned char*)v.outimage.data);
}
Expand Down
6 changes: 5 additions & 1 deletion RealSR-NCNN-Android-CLI/RealSR/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION
${ncnn_DIR}/${ANDROID_ABI}/lib/libncnn.so)


set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/OpenCV-android-sdk/sdk/native/jni/)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIR}/include)

#include_directories(${CMAKE_CURRENT_BINARY_DIR})
option(USE_SYSTEM_WEBP "build with system libwebp" OFF)
if(NOT USE_SYSTEM_WEBP)
Expand Down Expand Up @@ -48,4 +52,4 @@ add_executable(realsr-ncnn
realsr.cpp
)

target_link_libraries(realsr-ncnn webp ncnn)
target_link_libraries(realsr-ncnn webp ncnn ${OpenCV_LIBS})
27 changes: 26 additions & 1 deletion RealSR-NCNN-Android-CLI/RealSR/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ static std::vector<int> parse_optarg_int_array(const char *optarg) {
#include "realsr.h"

#include "filesystem_utils.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/hal/interface.h>
using namespace cv;

static void print_usage() {
fprintf(stderr, "Usage: realsr-ncnn -i infile -o outfile [options]...\n\n");
Expand Down Expand Up @@ -483,7 +486,29 @@ void *save(void *args) {

path_t ext = get_file_extension(v.outpath);

if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP")) {
if (ext != PATHSTR("gif")) {
// 使用opencv保存图片,速度比默认的stb更快
cv::Mat image;
switch (v.outimage.elempack) {
case 1:
image = cv::Mat( v.outimage.h, v.outimage.w, CV_8UC1, v.outimage.data); // 单通道图像
break;
case 3:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC3, v.outimage.data); // 3通道图像
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
break;
case 4:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC4, v.outimage.data); // 4通道图像
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
break;
}
if (image.empty()) {
std::cerr << "Error: Image data not loaded." << std::endl;
success = false;
} else {
success = imwrite(v.outpath.c_str(), image);
}
}else if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP")) {
success = webp_save(v.outpath.c_str(), v.outimage.w, v.outimage.h, v.outimage.elempack,
(const unsigned char *) v.outimage.data);
} else if (ext == PATHSTR("png") || ext == PATHSTR("PNG")) {
Expand Down
14 changes: 11 additions & 3 deletions RealSR-NCNN-Android-CLI/Resize/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ if (WIN32)
endif ()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../../Lib/valkan/Include)

set(ncnn_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/ncnn-windows-vs2019-shared)

add_library(ncnn SHARED IMPORTED)
Expand All @@ -25,11 +24,16 @@ if (WIN32)
${ncnn_DIR}/${TARG_CPU}/lib/ncnn.lib)
else ()
set(ncnn_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/ncnn-android-vulkan-shared)

add_library(ncnn SHARED IMPORTED)
include_directories(${ncnn_DIR}/${ANDROID_ABI}/include/ncnn)
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION
${ncnn_DIR}/${ANDROID_ABI}/lib/libncnn.so)

set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/OpenCV-android-sdk/sdk/native/jni/)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIR}/include)

endif ()
#set(libwebp_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libwebp)
set(libwebp_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/libwebp)
Expand Down Expand Up @@ -64,4 +68,8 @@ add_executable(resize-ncnn
main.cpp
)

target_link_libraries(resize-ncnn webp ncnn)
if (WIN32)
target_link_libraries(resize-ncnn webp ncnn)
else ()
target_link_libraries(resize-ncnn webp ncnn ${OpenCV_LIBS})
endif ()
34 changes: 32 additions & 2 deletions RealSR-NCNN-Android-CLI/Resize/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

#include "stb_image_write.h"

#include <opencv2/opencv.hpp>
#include <opencv2/core/hal/interface.h>
using namespace cv;

#endif // _WIN32

#include "webp_image.h"
Expand Down Expand Up @@ -829,12 +833,38 @@ int main(int argc, char **argv)
high_resolution_clock::time_point save_begin = high_resolution_clock::now();

{


int success = 0;

path_t ext = get_file_extension(imagepath);

#if _WIN32

#else
if (ext != PATHSTR("gif")) {
// 使用opencv保存图片,速度比默认的stb更快
cv::Mat image;
switch (c) {
case 1:
image = cv::Mat( out_h, out_w, CV_8UC1, buf); // 单通道图像
break;
case 3:
image = cv::Mat(out_h, out_w, CV_8UC3, buf); // 3通道图像
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
break;
case 4:
image = cv::Mat(out_h, out_w, CV_8UC4, buf); // 4通道图像
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
break;
}
if (image.empty()) {
std::cerr << "Error: Image data not loaded." << std::endl;
success = false;
} else {
success = imwrite(outputpath.c_str(), image);
fprintf(stderr, "opencv save image success, c=%d, w=%d, h=%d\n", c, w, h);
}
}else
#endif
if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP")) {
success = webp_save(outputpath.c_str(), out_w, out_h, c, buf);

Expand Down
6 changes: 5 additions & 1 deletion RealSR-NCNN-Android-CLI/SRMD/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION
${ncnn_DIR}/${ANDROID_ABI}/lib/libncnn.so)


set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/OpenCV-android-sdk/sdk/native/jni/)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIR}/include)

#include_directories(${CMAKE_CURRENT_BINARY_DIR})
option(USE_SYSTEM_WEBP "build with system libwebp" OFF)
if(NOT USE_SYSTEM_WEBP)
Expand Down Expand Up @@ -45,4 +49,4 @@ endif()

add_executable(srmd-ncnn main.cpp srmd.cpp)

target_link_libraries(srmd-ncnn webp ncnn)
target_link_libraries(srmd-ncnn webp ncnn ${OpenCV_LIBS})
27 changes: 26 additions & 1 deletion RealSR-NCNN-Android-CLI/SRMD/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static std::vector<int> parse_optarg_int_array(const char* optarg)
#include "srmd.h"

#include "filesystem_utils.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/hal/interface.h>
using namespace cv;

static void print_usage()
{
Expand Down Expand Up @@ -373,7 +376,29 @@ void* save(void* args)

path_t ext = get_file_extension(v.outpath);

if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
if (ext != PATHSTR("gif")) {
// 使用opencv保存图片,速度比默认的stb更快
cv::Mat image;
switch (v.outimage.elempack) {
case 1:
image = cv::Mat( v.outimage.h, v.outimage.w, CV_8UC1, v.outimage.data); // 单通道图像
break;
case 3:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC3, v.outimage.data); // 3通道图像
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
break;
case 4:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC4, v.outimage.data); // 4通道图像
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
break;
}
if (image.empty()) {
std::cerr << "Error: Image data not loaded." << std::endl;
success = false;
} else {
success = imwrite(v.outpath.c_str(), image);
}
}else if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
{
success = webp_save(v.outpath.c_str(), v.outimage.w, v.outimage.h, v.outimage.elempack, (const unsigned char*)v.outimage.data);
}
Expand Down
6 changes: 5 additions & 1 deletion RealSR-NCNN-Android-CLI/Waifu2x/src/main/jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION
${ncnn_DIR}/${ANDROID_ABI}/lib/libncnn.so)


set(OpenCV_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../3rdparty/OpenCV-android-sdk/sdk/native/jni/)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_DIR}/include)

#include_directories(${CMAKE_CURRENT_BINARY_DIR})
option(USE_SYSTEM_WEBP "build with system libwebp" OFF)
if(NOT USE_SYSTEM_WEBP)
Expand Down Expand Up @@ -44,4 +48,4 @@ endif()

add_executable(waifu2x-ncnn main.cpp waifu2x.cpp)

target_link_libraries(waifu2x-ncnn webp ncnn)
target_link_libraries(waifu2x-ncnn webp ncnn ${OpenCV_LIBS})
27 changes: 26 additions & 1 deletion RealSR-NCNN-Android-CLI/Waifu2x/src/main/jni/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static std::vector<int> parse_optarg_int_array(const char* optarg)
#include "waifu2x.h"

#include "filesystem_utils.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/hal/interface.h>
using namespace cv;

static void print_usage()
{
Expand Down Expand Up @@ -414,7 +417,29 @@ void* save(void* args)

path_t ext = get_file_extension(v.outpath);

if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
if (ext != PATHSTR("gif")) {
// 使用opencv保存图片,速度比默认的stb更快
cv::Mat image;
switch (v.outimage.elempack) {
case 1:
image = cv::Mat( v.outimage.h, v.outimage.w, CV_8UC1, v.outimage.data); // 单通道图像
break;
case 3:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC3, v.outimage.data); // 3通道图像
cv::cvtColor(image, image, cv::COLOR_RGB2BGR);
break;
case 4:
image = cv::Mat(v.outimage.h, v.outimage.w, CV_8UC4, v.outimage.data); // 4通道图像
cv::cvtColor(image, image, cv::COLOR_RGBA2BGRA);
break;
}
if (image.empty()) {
std::cerr << "Error: Image data not loaded." << std::endl;
success = false;
} else {
success = imwrite(v.outpath.c_str(), image);
}
}else if (ext == PATHSTR("webp") || ext == PATHSTR("WEBP"))
{
success = webp_save(v.outpath.c_str(), v.outimage.w, v.outimage.h, v.outimage.elempack, (const unsigned char*)v.outimage.data);
}
Expand Down
10 changes: 10 additions & 0 deletions RealSR-NCNN-Android-GUI/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions RealSR-NCNN-Android-GUI/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions RealSR-NCNN-Android-GUI/.idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion RealSR-NCNN-Android-GUI/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 571f9c9

Please sign in to comment.