Issue 1:
System information (version)

OpenCV => 3.4.1

Operating System / Platform => windows 10 x64

Compiler => mingw-w64 8.1.0 and mingw-w64 7.3.0

Detailed description

imwrite crash

test with mingw-w64 8.1.0 and mingw-w64 7.3.0

All same settings but OpenCV 3.4.0 is good.

Steps to reproduce

c++ code crash when inline matrix operation

#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char *argv[]) {
Mat1b img = Mat1b::zeros(300, 300);
imwrite("img.png", img + 50);
return 0;
}

this way don't crash

#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char *argv[]) {
Mat1b img = Mat1b::zeros(300, 300);
img = img + 50;
imwrite("img.png", img);
return 0;
}

cmake configuration

General configuration for OpenCV 3.4.1 =====================================
Version control: unknown

Platform:
Timestamp: 2018-06-04T06:00:53Z
Host: Windows 10.0.17134 AMD64
CMake: 3.11.2
CMake generator: MinGW Makefiles
CMake build tool: C:/msys64/mingw64/bin/mingw32-make.exe
Configuration: Release

CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation:
requested: SSE3

C/C++:
Built as dynamic libs?: NO
C++11: YES
C++ Compiler: C:/msys64/mingw64/bin/g++.exe (ver 7.3.0)
C++ flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C++ flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
C Compiler: C:/msys64/mingw64/bin/gcc.exe
C flags (Release): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -O3 -DNDEBUG -DNDEBUG
C flags (Debug): -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wno-implicit-fallthrough -fdiagnostics-show-option -Wno-long-long -fomit-frame-pointer -ffast-math -ffunction-sections -fdata-sections -msse -msse2 -msse3 -fvisibility=hidden -fopenmp -g -O0 -DDEBUG -D_DEBUG
Linker flags (Release): -Wl,--gc-sections
Linker flags (Debug): -Wl,--gc-sections
ccache: NO
Precompiled headers: NO
Extra dependencies: avifil32 avicap32 winmm msvfw32 comctl32 gdi32 ole32 setupapi ws2_32 opengl32 glu32
3rdparty dependencies: libprotobuf libjpeg libwebp libpng libtiff libjasper IlmImf zlib

OpenCV modules:
To be built: calib3d core dnn features2d flann highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python_bindings_generator shape stitching superres ts video videoio world
Disabled: js videostab
Disabled by dependency: -
Unavailable: cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
Applications: tests perf_tests apps
Documentation: NO
Non-free algorithms: NO

Windows RT support: NO

GUI:
Win32 UI: YES
OpenGL support: YES (opengl32 glu32)
VTK support: NO

Media I/O:
ZLib: build (ver 1.2.11)
JPEG: build (ver 90)
WEBP: build (ver encoder: 0x020e)
PNG: build (ver 1.6.34)
TIFF: build (ver 42 - 4.0.9)
JPEG 2000: build (ver 1.900.1)
OpenEXR: build (ver 1.7.1)

Video I/O:
Video for Windows: YES
DC1394: NO
FFMPEG: YES (prebuilt binaries)
avcodec: YES (ver 57.107.100)
avformat: YES (ver 57.83.100)
avutil: YES (ver 55.78.100)
swscale: YES (ver 4.8.100)
avresample: YES (ver 3.7.0)
GStreamer: NO
DirectShow: YES

Parallel framework: OpenMP

Trace: YES (built-in)

Other third-party libraries:
Lapack: NO
Eigen: NO
Custom HAL: NO
Protobuf: build (3.5.1)

NVIDIA CUDA: NO

OpenCL: YES (no extra features)
Include path: D:/software/opencv-3.4.1/3rdparty/include/opencl/1.2
Link libraries: Dynamic load

Python (for build): NO

Java:
ant: NO
JNI: NO
Java wrappers: NO
Java tests: NO

Matlab: NO

Install to: D:/software/cv/install
-----------------------------------------------------------------

Issue 2:
System information (version)

OpenCV => 3.4.1

Operating System / Platform => Ubuntu 18.04 64 Bit

Compiler => gcc 7.3.0

Detailed description

imwrite produces a segfault if the provided input array is an expression involving an operation on a mat, such as 2 * x or x + y. It occurs for me with both jpg and png output, and with 8U and 32F types. The problem does not occur on OpenCV 3.0.0, other versions untested.

Steps to reproduce

cv::Mat1b m = cv::Mat1b::zeros(10, 10);
cv::imwrite("works.png", cv::Mat(m * 2));
cv::imwrite("fails.png", m * 2);